Bug report #15715

Segmentation fault on exit when using a QgsMapTool

Added by Uros Bernik almost 5 years ago. Updated almost 5 years ago.

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


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


#1 Updated by Martin Dobias almost 5 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()


#2 Updated by Uros Bernik almost 5 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:


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

#3 Updated by Uros Bernik almost 5 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