From: Sandro Santilli Date: Fri, 24 Oct 2003 08:28:50 +0000 (+0000) Subject: Fixed memory leak in GEOSGetCoordinates(), made sure that GEOS2POSTGIS X-Git-Tag: pgis_0_8_0~57 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e7d49451191ac333c66a5c4441c501af42a82f95;p=postgis Fixed memory leak in GEOSGetCoordinates(), made sure that GEOS2POSTGIS free type string even in case of collapsed geoms. Made sure that geomunion release memory in case of exception thrown by GEOSUnion. Sooner release of palloced memory in PolyFromGeometry (pts_per_ring). git-svn-id: http://svn.osgeo.org/postgis/trunk@325 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/postgis_geos.c b/postgis_geos.c index 723e692a8..961a02e56 100644 --- a/postgis_geos.c +++ b/postgis_geos.c @@ -10,6 +10,12 @@ * ********************************************************************** * $Log$ + * Revision 1.13 2003/10/24 08:28:49 strk + * Fixed memory leak in GEOSGetCoordinates(), made sure that GEOS2POSTGIS + * free type string even in case of collapsed geoms. Made sure that geomunion + * release memory in case of exception thrown by GEOSUnion. Sooner release + * of palloced memory in PolyFromGeometry (pts_per_ring). + * * Revision 1.12 2003/10/16 20:16:18 dblasby * Added NOTICE_HANDLER function. For some reason this didnt get properly * committed last time. @@ -223,34 +229,28 @@ Datum geomunion(PG_FUNCTION_ARGS) if (g3 == NULL) { - elog(ERROR,"GEOS union() threw an error!"); GEOSdeleteGeometry(g1); GEOSdeleteGeometry(g2); + elog(ERROR,"GEOS union() threw an error!"); PG_RETURN_NULL(); //never get here } - //elog(NOTICE,"result: %s", GEOSasText(g3) ) ; result = GEOS2POSTGIS(g3, geom1->is3d || geom2->is3d); + GEOSdeleteGeometry(g1); + GEOSdeleteGeometry(g2); + GEOSdeleteGeometry(g3); + if (result == NULL) { - GEOSdeleteGeometry(g1); - GEOSdeleteGeometry(g2); - GEOSdeleteGeometry(g3); elog(ERROR,"GEOS union() threw an error (result postgis geometry formation)!"); PG_RETURN_NULL(); //never get here } - - - GEOSdeleteGeometry(g1); - GEOSdeleteGeometry(g2); - GEOSdeleteGeometry(g3); - - compressType(result); // convert multi* to single item if appropriate - PG_RETURN_POINTER(result); + compressType(result); // convert multi* to single item if appropriate + PG_RETURN_POINTER(result); } @@ -1126,6 +1126,7 @@ POLYGON3D *PolyFromGeometry(Geometry *g, int *size) GEOSdeleteChar( (char*) pts); + pfree(pts_per_ring); return poly; } @@ -1170,6 +1171,8 @@ GEOMETRY *GEOS2POSTGIS(Geometry *g,char want3d) { POINT3D *pt = GEOSGetCoordinate(g); + free(type); + result = make_oneobj_geometry(sizeof(POINT3D), (char *) pt, POINTTYPE, want3d, GEOSGetSRID(g),1.0, 0.0, 0.0 @@ -1177,7 +1180,6 @@ GEOMETRY *GEOS2POSTGIS(Geometry *g,char want3d) GEOSdeleteChar( (char*) pt); - free(type); return result; } else if (strcmp(type,"LineString")==0) @@ -1185,6 +1187,8 @@ GEOMETRY *GEOS2POSTGIS(Geometry *g,char want3d) LINE3D *line; int size; + free(type); + line = LineFromGeometry(g,&size); if (line == NULL) return NULL; @@ -1194,16 +1198,14 @@ GEOMETRY *GEOS2POSTGIS(Geometry *g,char want3d) (char *) line, LINETYPE, want3d, GEOSGetSRID(g),1.0, 0.0, 0.0 ); - free(type); return result; } else if (strcmp(type,"Polygon")==0) { - int size; POLYGON3D *poly; - + free(type); poly = PolyFromGeometry(g,&size); if (poly == NULL) @@ -1213,7 +1215,6 @@ GEOMETRY *GEOS2POSTGIS(Geometry *g,char want3d) (char *) poly, POLYGONTYPE, want3d, GEOSGetSRID(g),1.0, 0.0, 0.0 ); - free(type); return result; } else if (strcmp(type,"MultiPoint")==0) @@ -1222,6 +1223,8 @@ GEOMETRY *GEOS2POSTGIS(Geometry *g,char want3d) POINT3D *pts; GEOMETRY *g_new,*g_old; + free(type); + g_new = NULL; ngeoms = GEOSGetNumGeometries(g); @@ -1251,7 +1254,6 @@ GEOMETRY *GEOS2POSTGIS(Geometry *g,char want3d) bbox = bbox_of_geometry( g_new ); // make bounding box memcpy( &g_new->bvol, bbox, sizeof(BOX3D) ); // copy bounding box pfree( bbox ); // free bounding box - free(type); return g_new; } @@ -1261,6 +1263,7 @@ GEOMETRY *GEOS2POSTGIS(Geometry *g,char want3d) LINE3D *line; GEOMETRY *g_old; + free(type); ngeoms = GEOSGetNumGeometries(g); if (ngeoms ==0) @@ -1294,7 +1297,6 @@ GEOMETRY *GEOS2POSTGIS(Geometry *g,char want3d) bbox = bbox_of_geometry( result ); // make bounding box memcpy( &result->bvol, bbox, sizeof(BOX3D) ); // copy bounding box pfree( bbox ); // free bounding box - free(type); //elog(NOTICE,"end; %s",geometry_to_text(result)); //elog(NOTICE," size = %i", result->size); return result; @@ -1306,6 +1308,7 @@ GEOMETRY *GEOS2POSTGIS(Geometry *g,char want3d) POLYGON3D *poly; GEOMETRY *g_old; + free(type); // before we forget ;) ngeoms = GEOSGetNumGeometries(g); if (ngeoms ==0) @@ -1314,11 +1317,12 @@ GEOMETRY *GEOS2POSTGIS(Geometry *g,char want3d) result->type = MULTIPOLYGONTYPE; return result; } + for (t=0;tbvol, bbox, sizeof(BOX3D) ); // copy bounding box pfree( bbox ); // free bounding box - free(type); return result; } else if (strcmp(type,"GeometryCollection")==0) @@ -1345,6 +1348,8 @@ GEOMETRY *GEOS2POSTGIS(Geometry *g,char want3d) GEOMETRY *geom, *g2, *r; int t; + free(type); + if (ngeoms ==0) { result = makeNullGeometry(GEOSGetSRID(g)); @@ -1368,9 +1373,9 @@ GEOMETRY *GEOS2POSTGIS(Geometry *g,char want3d) pfree(r); pfree(g2); } - free(type); return geom; } + free(type); // Unknown geometry type return NULL; } diff --git a/postgis_geos_wrapper.cpp b/postgis_geos_wrapper.cpp index 421deaa91..3668119de 100644 --- a/postgis_geos_wrapper.cpp +++ b/postgis_geos_wrapper.cpp @@ -2,6 +2,12 @@ /* * $Log$ +* Revision 1.11 2003/10/24 08:28:50 strk +* Fixed memory leak in GEOSGetCoordinates(), made sure that GEOS2POSTGIS +* free type string even in case of collapsed geoms. Made sure that geomunion +* release memory in case of exception thrown by GEOSUnion. Sooner release +* of palloced memory in PolyFromGeometry (pts_per_ring). +* * Revision 1.10 2003/10/20 19:50:49 strk * Removed some memory leaks in PostGIS2* converters. * @@ -1083,7 +1089,8 @@ POINT3D *GEOSGetCoordinates(Geometry *g1) int numPoints = g1->getNumPoints(); POINT3D *result = (POINT3D*) malloc (sizeof(POINT3D) * numPoints ); int t; - CoordinateList *cl = g1->getCoordinates(); + LineString *ls = (LineString *)g1; + const CoordinateList *cl = ls->getCoordinatesRO(); Coordinate c; for (t=0;t