Bug report #777

crashes with python reference to destroyed objects (e.g exportToWkt() crashes when geom has been deleted)

Added by crschmidt - about 17 years ago. Updated over 8 years ago.

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

Description

When a feature is destroyed, if it has ownership over a geometry, it deletes that geometry. After that point, if one tries to call functions on the geometry, it will return nulls (Linux) or cause a KERN_PROTECTION_FAILURE (OS X).

This can be reproduced by selecting a feature, then typing the following into the Python console:

iface.activeLayer().selectedFeatures()r0.geometry().exportToWkt()

(Sometimes it requires calling it twice to reproduce the crash -- however, it will never return the correct answer.)

The reason for this appears to be that the feature created when it is pulled out of the list is then destoryed before exportToWkt() is called, taking the geometry with it.

A workaround is to instead call geometryAndOwnership(), which tells the feature to not destroy the geometry.

It seems like this problem may actually be exportGeosToWkb -- functions like wkbType() fail in the same way. It's possible that the mGeometry check at the beginning of these functions needs to move before teh exportGeosToWkb(), or that the exportGeosToWkb() needs to more resilient against being deleted.


Related issues

Related to QGIS Application - Bug report #10755: Python Console crashes when removing non-existent vector ... Closed 2014-06-30
Duplicated by QGIS Application - Bug report #7228: Incorrect return on boundingbox one-liner Closed 2013-02-24
Duplicated by QGIS Application - Bug report #9185: Crash when perform some commands in Python console Closed 2013-12-09
Duplicated by QGIS Application - Bug report #13084: Segfault when accessing geometry from Python console Closed 2015-07-06
Duplicated by QGIS Application - Bug report #14320: Qgis crashes when trying to add vector layer Closed 2016-02-17

Associated revisions

Revision bd7d9133
Added by Nyall Dawson over 8 years ago

Refine QgsFeature geometry getters/setters

All pointer based methods have been removed.

Now we have only:

void setGeometry( const QgsGeometry& geom )

and

QgsGeometry geometry() const

Benefits include avoiding a whole lot of tricky pointer lifetime
issues, potential memory leaks, and finally closing #777, which
has survived for over 9 years!...

Impacts on PyQGIS code:
- no more need for the messy
g = QgsGeometry( feature.geometry() )
workaround, just use g = feature.geometry() instead
- IMPORTANT: you can no longer test whether a feature has geometry
using `if f.geometry():`, since QgsFeature::geometry() will
always return an object. Instead, use
`if not f.geometry().isEmpty():`, or preferably the new method
`if not f.hasGeometry():`

Fix #777

History

#1 Updated by Martin Dobias about 17 years ago

The problem here is in python bindings because the scenario seems to be like this:
1. get feature
2. store geometry's reference in Python
3. feature is deleted (together with geometry)
4. reference in Python still exists, but the object it's pointing to doesn't

I'm trying to find out how to cope with this correctly...

Martin

#2 Updated by Jürgen Fischer about 16 years ago

see also #1248

#3 Updated by Giovanni Manghi over 15 years ago

Hi,

what is the status of this issue?

cheers

#4 Updated by Jürgen Fischer almost 15 years ago

see also #2173

#5 Updated by Paolo Cavallini over 14 years ago

Still true?

#6 Updated by Martin Dobias over 14 years ago

This haven't been fixed yet

#7 Updated by Giuseppe Sucameli over 14 years ago

No crashes in my Ubuntu 9.04, but I never get the correct results.

I tried wkbType() on the same selected geometry a lot of times and I get different (and also strange) results:
0, 16777216, 7, 187101, 92, 143587, ...

#8 Updated by Giovanni Manghi almost 13 years ago

  • Target version changed from Version 1.7.0 to Version 1.7.4

#9 Updated by Giovanni Manghi almost 13 years ago

  • Pull Request or Patch supplied set to No
  • Crashes QGIS or corrupts data set to Yes
  • Affected QGIS version set to master

#10 Updated by Paolo Cavallini over 12 years ago

  • Target version changed from Version 1.7.4 to Version 1.8.0

#11 Updated by Paolo Cavallini about 12 years ago

  • Target version changed from Version 1.8.0 to Version 2.0.0

#12 Updated by Giovanni Manghi almost 12 years ago

  • Priority changed from Low to High

#13 Updated by Matthias Kuhn over 11 years ago

Fix in pull request #436

Not sure if it's the best way to do it.
Maybe a reference counter or the like could also help to overcome this problem. But I'm not sure how easy it is to do mixed ref-counting between python and C++?

#14 Updated by Paolo Cavallini almost 11 years ago

  • Target version changed from Version 2.0.0 to Future Release - High Priority

#15 Updated by Jürgen Fischer over 8 years ago

  • Assignee deleted (Martin Dobias)

#16 Updated by Nyall Dawson over 8 years ago

  • Status changed from Open to Closed

Also available in: Atom PDF