Bug report #777

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

Added by crschmidt - over 9 years ago. Updated 7 months ago.

Status:Closed Start Date:
Priority:High Due date:
Assigned to:- % Done:

0%

Category:Python plugins and bindings
Target version:Future Release - High Priority
Platform:All Pull Request or Patch supplied:No
Platform version: Affected version:master
Status info:0 Causes crash or corruption:Yes
Resolution: Tag:

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 06/30/2014
duplicated by QGIS Application - Bug report #13084: Segfault when accessing geometry from Python console Closed 07/06/2015
duplicated by QGIS Application - Bug report #9185: Crash when perform some commands in Python console Closed 12/09/2013
duplicated by QGIS Application - Bug report #7228: Incorrect return on boundingbox one-liner Closed 02/24/2013
duplicated by QGIS Application - Bug report #14320: Qgis crashes when trying to add vector layer Closed 02/17/2016

Associated revisions

Revision bd7d913379b68a8104608b1afab4d380e4edc26b
Added by Nyall Dawson 7 months 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

Updated by Martin Dobias over 9 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

Updated by Jürgen Fischer over 8 years ago

see also #1248

Updated by Giovanni Manghi over 7 years ago

Hi,

what is the status of this issue?

cheers

Updated by Jürgen Fischer about 7 years ago

see also #2173

Updated by Paolo Cavallini over 6 years ago

Still true?

Updated by Martin Dobias over 6 years ago

This haven't been fixed yet

Updated by Giuseppe Sucameli over 6 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, ...

Updated by Giovanni Manghi about 5 years ago

  • Target version changed from Version 1.7.0 to Version 1.7.4

Updated by Giovanni Manghi about 5 years ago

  • Pull Request or Patch supplied set to No
  • Affected version set to master
  • Causes crash or corruption set to Yes

Updated by Paolo Cavallini almost 5 years ago

  • Target version changed from Version 1.7.4 to Version 1.8.0

Updated by Paolo Cavallini over 4 years ago

  • Target version changed from Version 1.8.0 to Version 2.0.0

Updated by Giovanni Manghi about 4 years ago

  • Priority changed from Low to High

Updated by Matthias Kuhn about 4 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++?

Updated by Paolo Cavallini about 3 years ago

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

Updated by Jürgen Fischer 8 months ago

  • Assigned to deleted (Martin Dobias)

Updated by Nyall Dawson 7 months ago

  • Status changed from Open to Closed

Also available in: Atom