]> granicus.if.org Git - postgis/commitdiff
Fixed memory leak in GEOSGetCoordinates(), made sure that GEOS2POSTGIS
authorSandro Santilli <strk@keybit.net>
Fri, 24 Oct 2003 08:28:50 +0000 (08:28 +0000)
committerSandro Santilli <strk@keybit.net>
Fri, 24 Oct 2003 08:28:50 +0000 (08:28 +0000)
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

postgis_geos.c
postgis_geos_wrapper.cpp

index 723e692a8807c55d384edf6b848607ec5aa87cd2..961a02e5609beae5e70a21adb3afa6055c630519 100644 (file)
  *
  **********************************************************************
  * $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;t<ngeoms;t++)
                {
+                       poly = PolyFromGeometry(GEOSGetGeometryN(g,t) ,&size);
                        if (t==0)
                        {
-                               poly = PolyFromGeometry(GEOSGetGeometryN(g,0) ,&size);
                                result = make_oneobj_geometry(size,
                                                        (char *) poly,
                                                           MULTIPOLYGONTYPE,  want3d, GEOSGetSRID(g),1.0, 0.0, 0.0
@@ -1326,16 +1330,15 @@ GEOMETRY *GEOS2POSTGIS(Geometry *g,char want3d)
                        }
                        else
                        {
-                               poly = PolyFromGeometry(GEOSGetGeometryN(g,t) ,&size);
                                g_old = result;
                                result =        add_to_geometry(g_old,size, (char*) poly, POLYGONTYPE);
                                pfree(g_old);
                        }
+                       pfree(poly);
                }
                bbox = bbox_of_geometry( result ); // make bounding box
            memcpy( &result->bvol, 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;
 }
 
index 421deaa91a57f5afa420cc501bcd229e943cf8c9..3668119de4a5dcf0d96fdcf04f27819b66a1c31b 100644 (file)
@@ -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<numPoints;t++)