Bug report #13616

Pyqgis QgsFeature.setGeometry() causes crash

Added by Spencer Gardner over 8 years ago. Updated over 8 years ago.

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

Description

I'm getting a hard crash when I call setGeometry from the processing framework. I can't figure out why my code is failing but in any case QGIS shouldn't be crashing like this. Looks to be a segfault based on the log.

segfault at 0 ip 00007fc63a2a846a sp 00007fff34f66f00 error 6 in libqgis_core.so.2.10.1[7fc63a14d000+7ce000]

A few odd things:
1) It doesn't crash all the time. The sample code below seems to crash about 80% of the time. But every once in a while it works without an issue. And when it works, I can run it again during the same session without problems. But if I restart QGIS it crashes again.
2) I also tried using the WKT of the geometry in the sample below. If I construct the geom from the WKT there is no crash. For reference, the WKT of the sample feature is "LINESTRING" - the SRID is 2283.

Sample code (from the Python console)

 #- note that roadsLayer is a PostGIS layer of type LINESTRING and primary key of "road_id")
 #- and the code grabs a geom from one of the road features by looking up the geom
 #- in a dictionary with keys of road_ids and values of geoms.
 #- I've chosen an arbitrary road_id to illustrate.

 #- imports
from qgis.core import *
from PyQt4.QtCore import QSettings, QVariant
from processing.tools import vector

 #- fields
fields = QgsFields()
fields.append(QgsField('field1', QVariant.Int))

 #- dict of road IDs and geoms (for convenience)
roads = dict()
for feat in vector.features(roadsLayer):
    roads[feat['road_id']] = feat.geometry()

 #- create the new feature
f = QgsFeature(fields)
f.setAttribute(0,0)
roadId = 6985
f.setGeometry(roads.get(roadId))

History

#1 Updated by Nyall Dawson over 8 years ago

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

Duplicate of #777.

Solution is to alter this part of your script:

for feat in vector.features(roadsLayer):
roads[feat['road_id']] = feat.geometry()

to

for feat in vector.features(roadsLayer):
roads[feat['road_id']] = QgsGeometry(feat.geometry())

Also available in: Atom PDF