@@ -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)
0 commit comments