Skip to content

Commit c11b1c8

Browse files
committed
Attempt to fix memory leak
1 parent 1a3954d commit c11b1c8

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

pygeos/_geometry.pyx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,6 @@ def polygons_1d(object shells, object holes, object indices):
395395
result_view[poly_idx] = PyGEOS_CreateGeometry(NULL, geos_handle)
396396
continue
397397

398-
# clone the shell as the polygon will take ownership
399-
shell = GEOSGeom_clone_r(geos_handle, shell)
400-
if shell == NULL:
401-
return # GEOSException is raised by get_geos_handle
402-
403398
# fill the temporary array with holes belonging to this polygon
404399
for poly_hole_idx in range(hole_count[poly_idx]):
405400
if PyGEOS_GetGEOSGeometry(<PyObject *>holes_view[hole_idx_1 + poly_hole_idx], &hole) == 0:
@@ -420,6 +415,12 @@ def polygons_1d(object shells, object holes, object indices):
420415
temp_holes_view[n_holes] = <np.intp_t>hole
421416
n_holes += 1
422417

418+
# clone the shell as the polygon will take ownership
419+
shell = GEOSGeom_clone_r(geos_handle, shell)
420+
if shell == NULL:
421+
_deallocate_arr(geos_handle, temp_holes_view, n_holes)
422+
return # GEOSException is raised by get_geos_handle
423+
423424
# create the polygon
424425
poly = GEOSGeom_createPolygon_r(
425426
geos_handle,
@@ -428,6 +429,10 @@ def polygons_1d(object shells, object holes, object indices):
428429
n_holes
429430
)
430431
if poly == NULL:
432+
# GEOSGeom_createPolygon_r does not take ownership in case of
433+
# an exception: so clean up the provided arguments.
434+
GEOSGeom_destroy_r(geos_handle, shell)
435+
_deallocate_arr(geos_handle, temp_holes_view, n_holes)
431436
return # GEOSException is raised by get_geos_handle
432437

433438
result_view[poly_idx] = PyGEOS_CreateGeometry(poly, geos_handle)

pygeos/test/test_creation_indices.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
pnts = pygeos.points
1414
lstrs = pygeos.linestrings
15-
poly = pygeos.polygons
1615
geom_coll = pygeos.geometrycollections
1716

1817

0 commit comments

Comments
 (0)