gdal icon indicating copy to clipboard operation
gdal copied to clipboard

Python bindings: Segfault when accessing sub-geometry after parent has been destroyed

Open dbaston opened this issue 1 year ago • 1 comments

What is the bug?

Accessing a sub-geometry after its parent has been destroyed causes a segfault. The sub-geometry should either keep a strong reference on its parent (may lead to unexpected behavior somewhere?) or the parent should invalidate references to its children on destruction.

Steps to reproduce the issue

g = ogr.CreateGeometryFromWkt("Point (0 0)")
q = g.Buffer(1).GetGeometryRef(0)
q.GetPointCount() # segfault

Versions and provenance

master

Additional context

No response

dbaston avatar May 14 '24 19:05 dbaston

I don't see a particular issue in trying to keep alive the parent in GetGeometryRef().

Not sure if you're aware of it, but ther are complications regarding life-time of geometries with RemoveGeometry(). But that's a more advanced/rare use case than the one of g.Buffer(1).GetGeometryRef(0), so it could be left aside

>>> from osgeo import ogr
>>> g = ogr.CreateGeometryFromWkt("MultiPoint ((0 0))")
>>> point = g.GetGeometryRef(0)
>>> g.RemoveGeometry(0)
>>> point.ExportToWkt()
Segmentation fault (core dumped)

rouault avatar May 14 '24 19:05 rouault

Related: https://github.com/OSGeo/gdal/issues/4401

dbaston avatar May 29 '24 18:05 dbaston