Bug report #15715

Segmentation fault on exit when using a QgsMapTool

Added by Uros Bernik about 8 years ago. Updated about 8 years ago.

Status:Closed
Priority:High
Assignee:-
Category:Python plugins
Affected QGIS version:2.16.3 Regression?:No
Operating System:Ubuntu Easy fix?:No
Pull Request or Patch supplied:No Resolution:invalid
Crashes QGIS or corrupts data:No Copied to github as #:23638

Description

When using pyqgis in a custom application, there is a Segmentation fault crash upon exiting the application after app.exitQGis() call when the programm exists. It appears on exit as soon as during lifetime of the application a QgsMapToolPan (as in example) is created. During application lifetime itself, there are no problems.

qgis-bug.py Magnifier - Minimum standalone example to reproduce the bug (1.27 KB) Uros Bernik, 2016-10-17 03:53 AM

History

#1 Updated by Martin Dobias about 8 years ago

  • Resolution set to invalid
  • Status changed from Open to Closed

It looks like your "gui" variable with canvas and a map tool still lives when QGIS libs have been already uninitialized. That means when cleaning up the "gui" variable you get the crash. A simple fix should be to delete "gui" before calling exitQgis(). Or just put all handling of "gui" variable into a function, so it gets out of scope before exitQgis():

def run_app(app):
  gui = GUI()
  gui.show()
  app.exec_()

qgsapp.initQgis()
run_app(qgsapp)
qgsapp.exitQgis()

#2 Updated by Uros Bernik about 8 years ago

Yes, i've seen this suggested: http://gis.stackexchange.com/questions/206523/segmentation-fault-upon-exit-of-qgis-map-canvas

The error still occurs, even with the aux function implementation, the problematic line is the creation of a pan tool, more precisely, if a pan tool instance is stored into a gui instance variable.

If it's just created in the construcor then set as the current tool, there is no segfault on exit:

self._map.setMapTool(QgsMapToolPan(self._map))

For the moment, im unsetting the GUI instance variable at window close, which also works.

#3 Updated by Uros Bernik about 8 years ago

Ah, yes, I have an idea why. If an object belonging to the canvas is referenced somewhere "outside" of qgis context as well, it is probably not properly destroyed, when exitQgis is called, hence the segfault.

Also available in: Atom PDF