]> granicus.if.org Git - postgis/commitdiff
lwgeom_release (#699), reviewed all usage of lwgeom_release
authorPaul Ramsey <pramsey@cleverelephant.ca>
Tue, 7 Feb 2012 19:42:49 +0000 (19:42 +0000)
committerPaul Ramsey <pramsey@cleverelephant.ca>
Tue, 7 Feb 2012 19:42:49 +0000 (19:42 +0000)
and replaced with lwgeom_free where possible. Changed the
def'n of lwgeom_release so that it only frees the containing
objects, and not any sub-objects, for those few remaining
cases that required a gentler freeing of objects.

git-svn-id: http://svn.osgeo.org/postgis/trunk@9072 b70326c6-7e19-0410-871a-916f4a2858ee

22 files changed:
liblwgeom/cunit/cu_split.c
liblwgeom/lwcircstring.c
liblwgeom/lwcollection.c
liblwgeom/lwgeom.c
liblwgeom/lwgeom_geos_node.c
liblwgeom/lwline.c
liblwgeom/lwmline.c
liblwgeom/lwmpoint.c
liblwgeom/lwmpoly.c
liblwgeom/lwpoint.c
liblwgeom/lwpoly.c
liblwgeom/lwpsurface.c
liblwgeom/lwtin.c
liblwgeom/lwtriangle.c
postgis/geography_measurement.c
postgis/lwgeom_functions_basic.c
postgis/lwgeom_functions_lrs.c
postgis/lwgeom_geos.c
postgis/lwgeom_ogc.c
postgis/lwgeom_rtree.c
postgis/lwgeom_spheroid.c
postgis/lwgeom_sqlmm.c

index cc81b7b3bed9953e4c8866a2f3b882b2ff705782..adfbdaf06064220c828b4172e0a37a0e1b3b4b05 100644 (file)
@@ -40,7 +40,7 @@ static void test_lwline_split_by_point_to(void)
        ret = lwline_split_by_point_to(line, point, coll);
        CU_ASSERT_EQUAL(ret, 1);
        CU_ASSERT_EQUAL(coll->ngeoms, 0);
-       lwgeom_free((LWGEOM*)point);
+       lwpoint_free(point);
 
        point = lwgeom_as_lwpoint(lwgeom_from_wkt(
                "POINT(10 0)",
@@ -48,7 +48,7 @@ static void test_lwline_split_by_point_to(void)
        ret = lwline_split_by_point_to(line, point, coll);
        CU_ASSERT_EQUAL(ret, 1);
        CU_ASSERT_EQUAL(coll->ngeoms, 0);
-       lwgeom_free((LWGEOM*)point);
+       lwpoint_free(point);
 
        point = lwgeom_as_lwpoint(lwgeom_from_wkt(
                "POINT(5 0)",
@@ -56,7 +56,7 @@ static void test_lwline_split_by_point_to(void)
        ret = lwline_split_by_point_to(line, point, coll);
        CU_ASSERT_EQUAL(ret, 0);
        CU_ASSERT_EQUAL(coll->ngeoms, 0);
-       lwgeom_free((LWGEOM*)point);
+       lwpoint_free(point);
 
        point = lwgeom_as_lwpoint(lwgeom_from_wkt(
                "POINT(5 5)",
@@ -64,7 +64,7 @@ static void test_lwline_split_by_point_to(void)
        ret = lwline_split_by_point_to(line, point, coll);
        CU_ASSERT_EQUAL(ret, 2);
        CU_ASSERT_EQUAL(coll->ngeoms, 2);
-       lwgeom_free((LWGEOM*)point);
+       lwpoint_free(point);
 
        point = lwgeom_as_lwpoint(lwgeom_from_wkt(
                "POINT(2 2)",
@@ -72,11 +72,10 @@ static void test_lwline_split_by_point_to(void)
        ret = lwline_split_by_point_to(line, point, coll);
        CU_ASSERT_EQUAL(ret, 2);
        CU_ASSERT_EQUAL(coll->ngeoms, 4);
-       lwgeom_free((LWGEOM*)point);
+       lwpoint_free(point);
 
-       lwgeom_free((LWGEOM*)coll);
-
-       lwgeom_free((LWGEOM*)line);
+       lwcollection_free(coll);
+       lwline_free(line);
 
 #endif /* POSTGIS_GEOS_VERSION >= 33 */
 }
index 01a7a61475dcea7c863ffdb539e66563511278c8..86b76ee8041c013fbe4454206273cdbdd3cbd33e 100644 (file)
@@ -84,6 +84,8 @@ lwcircstring_release(LWCIRCSTRING *lwcirc)
 
 void lwcircstring_free(LWCIRCSTRING *curve)
 {
+       if ( ! curve ) return;
+       
        if ( curve->bbox )
                lwfree(curve->bbox);
        if ( curve->points )
index 17d273061c2377312a78e38a8b9864535606b360..2adeb08475786b4daab6eb6050fabe58c48ecc7f 100644 (file)
@@ -316,6 +316,8 @@ int lwcollection_ngeoms(const LWCOLLECTION *col)
 void lwcollection_free(LWCOLLECTION *col)
 {
        int i;
+       if ( ! col ) return;
+       
        if ( col->bbox )
        {
                lwfree(col->bbox);
index 409c0f8e92530a0b315b3afccbe3c65ac4a0ee9f..98215aea81edd4529b07ad9930b6c755d52b4d5b 100644 (file)
@@ -327,12 +327,16 @@ lwgeom_as_multi(const LWGEOM *lwgeom)
        return ogeom;
 }
 
+
+/**
+* Free the containing LWGEOM and the associated BOX. Leave the underlying 
+* geoms/points/point objects intact. Useful for functions that are stripping
+* out subcomponents of complex objects, or building up new temporary objects
+* on top of subcomponents.
+*/
 void
 lwgeom_release(LWGEOM *lwgeom)
 {
-       uint32_t i;
-       LWCOLLECTION *col;
-
        if ( ! lwgeom )
                lwerror("lwgeom_release: someone called on 0x0");
 
@@ -342,24 +346,9 @@ lwgeom_release(LWGEOM *lwgeom)
        if ( lwgeom->bbox )
        {
                LWDEBUGF(3, "lwgeom_release: releasing bbox. %p", lwgeom->bbox);
-
                lwfree(lwgeom->bbox);
        }
-
-       /* Collection */
-       if ( (col=lwgeom_as_lwcollection(lwgeom)) )
-       {
-               LWDEBUG(3, "lwgeom_release: Releasing collection.");
-
-               for (i=0; i<col->ngeoms; i++)
-               {
-                       lwgeom_release(col->geoms[i]);
-               }
-               lwfree(lwgeom);
-       }
-
-       /* Single element */
-       else lwfree(lwgeom);
+       lwfree(lwgeom);
 
 }
 
index ab0fb58346d320991a26da48e296f9d007c09b31..7cde738657ff0aa025ae333c306709072878523f 100644 (file)
@@ -96,12 +96,11 @@ lwgeom_extract_unique_endpoints(const LWGEOM* lwg)
        GEOSGeometry *gepu;
        LWMPOINT *epall = lwgeom_extract_endpoints(lwg);
        GEOSGeometry *gepall = LWGEOM2GEOS((LWGEOM*)epall);
+       lwmpoint_free(epall);
        if ( ! gepall ) {
-               lwgeom_free((LWGEOM*)epall);
                lwerror("LWGEOM2GEOS: %s", lwgeom_geos_errmsg);
                return NULL;
        }
-       lwgeom_free((LWGEOM*)epall);
 
        /* UnaryUnion to remove duplicates */
        /* TODO: do it all within pgis using indices */
@@ -251,7 +250,7 @@ lwgeom_node(const LWGEOM* lwgeom_in)
        }
 
        lwgeom_free(ep);
-       lwgeom_free((LWGEOM*)col);
+       lwcollection_free(col);
 
        lines->srid = lwgeom_in->srid;
        return (LWGEOM*)lines;
index 74e878af1dacbe3458252765c0555368d3b1aedc..a920f9eab3c79adfea86ee3c9196bf97b9b9cf80 100644 (file)
@@ -61,6 +61,8 @@ lwline_construct_empty(int srid, char hasz, char hasm)
 
 void lwline_free (LWLINE  *line)
 {
+       if ( ! line ) return;
+       
        if ( line->bbox )
                lwfree(line->bbox);
        if ( line->points )
index da3134814918bf42af49a925d8c85f5a2bf8ef00..9e84e5c9b4b610b20cfe50bb3ebf9587663373a8 100644 (file)
@@ -99,6 +99,8 @@ lwmline_measured_from_lwmline(const LWMLINE *lwmline, double m_start, double m_e
 void lwmline_free(LWMLINE *mline)
 {
        int i;
+       if ( ! mline ) return;
+       
        if ( mline->bbox )
                lwfree(mline->bbox);
 
index 72c44284c54c2b9333e1e6b192a385ad9742d059..9535f7285eb155159c6ebbf099eda0112b5be5a7 100644 (file)
@@ -59,6 +59,9 @@ lwmpoint_construct(int srid, const POINTARRAY *pa)
 void lwmpoint_free(LWMPOINT *mpt)
 {
        int i;
+
+       if ( ! mpt ) return;
+       
        if ( mpt->bbox )
                lwfree(mpt->bbox);
 
index 730ca4b6b2b623ef7b97d02e1adc0d92b61e2d34..c430abed40b5d011cdb75e937e56ff11101efb15 100644 (file)
@@ -40,6 +40,7 @@ LWMPOLY* lwmpoly_add_lwpoly(LWMPOLY *mobj, const LWPOLY *obj)
 void lwmpoly_free(LWMPOLY *mpoly)
 {
        int i;
+       if ( ! mpoly ) return;
        if ( mpoly->bbox )
                lwfree(mpoly->bbox);
 
index c56a4cf6db2f2a0b48e3ba88a012c509e6c6fc45..28f25632f40a6de51f015eb54453ebde5c2751a8 100644 (file)
@@ -179,6 +179,8 @@ lwpoint_make(int srid, int hasz, int hasm, const POINT4D *p)
 
 void lwpoint_free(LWPOINT *pt)
 {
+       if ( ! pt ) return;
+       
        if ( pt->bbox )
                lwfree(pt->bbox);
        if ( pt->point )
index d10941030a51beef18ea469200ee066b99b51ee9..acafe12771c69607e01f7f67d466a281d8a994bb 100644 (file)
@@ -79,6 +79,8 @@ void lwpoly_free(LWPOLY  *poly)
 {
        int t;
 
+       if( ! poly ) return;
+
        if ( poly->bbox )
                lwfree(poly->bbox);
 
index 2732f1b05782a8c4cb4adc73532d38c47d54b4e0..37f038d913585ccb750c28ce52c9a07944ee7af7 100644 (file)
@@ -26,7 +26,7 @@ LWPSURFACE* lwpsurface_add_lwpoly(LWPSURFACE *mobj, const LWPOLY *obj)
 void lwpsurface_free(LWPSURFACE *psurf)
 {
        int i;
-
+       if ( ! psurf ) return;
        if ( psurf->bbox )
                lwfree(psurf->bbox);
 
index 12149a94d228b61e158a4dfc943ebb5dfe8c7fa9..d7fef8524db127fc04f5abb28981bfcb476a49de 100644 (file)
@@ -27,7 +27,7 @@ LWTIN* lwtin_add_lwtriangle(LWTIN *mobj, const LWTRIANGLE *obj)
 void lwtin_free(LWTIN *tin)
 {
        int i;
-
+       if ( ! tin ) return;
        if ( tin->bbox )
                lwfree(tin->bbox);
 
index 644d21e2edd20a935017ca4e78e44cc00f025019..22dc408b62c12990f919c595f08ce32be926570c 100644 (file)
@@ -55,6 +55,8 @@ lwtriangle_construct_empty(int srid, char hasz, char hasm)
 
 void lwtriangle_free(LWTRIANGLE  *triangle)
 {
+       if ( ! triangle ) return;
+       
        if (triangle->bbox)
                lwfree(triangle->bbox);
                
index e31a4d35fd25b254e19f0b97af5c428bd790523d..063e350ac1355deb4e8ab585bab6306308f7426d 100644 (file)
@@ -84,22 +84,19 @@ Datum geography_distance(PG_FUNCTION_ARGS)
 
        distance = lwgeom_distance_spheroid(lwgeom1, lwgeom2, &s, FP_TOLERANCE);
 
+       /* Clean up */
+       lwgeom_free(lwgeom1);
+       lwgeom_free(lwgeom2);
+       PG_FREE_IF_COPY(g1, 0);
+       PG_FREE_IF_COPY(g2, 1);
+
        /* Something went wrong, negative return... should already be eloged, return NULL */
        if ( distance < 0.0 )
        {
-               PG_FREE_IF_COPY(g1, 0);
-               PG_FREE_IF_COPY(g2, 1);
                PG_RETURN_NULL();
        }
 
-       /* Clean up, but not all the way to the point arrays */
-       lwgeom_release(lwgeom1);
-       lwgeom_release(lwgeom2);
-
-       PG_FREE_IF_COPY(g1, 0);
-       PG_FREE_IF_COPY(g2, 1);
        PG_RETURN_FLOAT8(distance);
-
 }
 
 /*
@@ -146,6 +143,12 @@ Datum geography_dwithin(PG_FUNCTION_ARGS)
 
        distance = lwgeom_distance_spheroid(lwgeom1, lwgeom2, &s, tolerance);
 
+       /* Clean up */
+       lwgeom_free(lwgeom1);
+       lwgeom_free(lwgeom2);
+       PG_FREE_IF_COPY(g1, 0);
+       PG_FREE_IF_COPY(g2, 1);
+
        /* Something went wrong... should already be eloged, return FALSE */
        if ( distance < 0.0 )
        {
@@ -153,12 +156,6 @@ Datum geography_dwithin(PG_FUNCTION_ARGS)
                PG_RETURN_BOOL(FALSE);
        }
 
-       /* Clean up, but not all the way to the point arrays */
-       lwgeom_release(lwgeom1);
-       lwgeom_release(lwgeom2);
-
-       PG_FREE_IF_COPY(g1, 0);
-       PG_FREE_IF_COPY(g2, 1);
        PG_RETURN_BOOL(distance < tolerance);
 }
 
@@ -229,7 +226,7 @@ Datum geography_area(PG_FUNCTION_ARGS)
        /* EMPTY things have no area */
        if ( lwgeom_is_empty(lwgeom) )
        {
-               lwgeom_release(lwgeom);
+               lwgeom_free(lwgeom);
                PG_RETURN_FLOAT8(0.0);
        }
        
@@ -259,6 +256,10 @@ Datum geography_area(PG_FUNCTION_ARGS)
        else
                area = lwgeom_area_sphere(lwgeom, &s);
 
+       /* Clean up */
+       lwgeom_free(lwgeom);
+       PG_FREE_IF_COPY(g, 0);
+
        /* Something went wrong... */
        if ( area < 0.0 )
        {
@@ -266,12 +267,7 @@ Datum geography_area(PG_FUNCTION_ARGS)
                PG_RETURN_NULL();
        }
 
-       /* Clean up, but not all the way to the point arrays */
-       lwgeom_release(lwgeom);
-
-       PG_FREE_IF_COPY(g, 0);
        PG_RETURN_FLOAT8(area);
-
 }
 
 /*
@@ -463,8 +459,8 @@ Datum geography_covers(PG_FUNCTION_ARGS)
        /* EMPTY never intersects with another geometry */
        if ( lwgeom_is_empty(lwgeom1) || lwgeom_is_empty(lwgeom2) )
        {
-               lwgeom_release(lwgeom1);
-               lwgeom_release(lwgeom2);
+               lwgeom_free(lwgeom1);
+               lwgeom_free(lwgeom2);
                PG_FREE_IF_COPY(g1, 0);
                PG_FREE_IF_COPY(g2, 1);
                PG_RETURN_BOOL(false);
@@ -473,12 +469,12 @@ Datum geography_covers(PG_FUNCTION_ARGS)
        /* Calculate answer */
        result = lwgeom_covers_lwgeom_sphere(lwgeom1, lwgeom2);
 
-       /* Clean up, but not all the way to the point arrays */
-       lwgeom_release(lwgeom1);
-       lwgeom_release(lwgeom2);
-
+       /* Clean up */
+       lwgeom_free(lwgeom1);
+       lwgeom_free(lwgeom2);
        PG_FREE_IF_COPY(g1, 0);
        PG_FREE_IF_COPY(g2, 1);
+
        PG_RETURN_BOOL(result);
 }
 
index 92e98de303d71d836cdd9b02bb8743bae1ce650b..14cef5da4f88ba8f4f0dc68f2626c090cf33e4c2 100644 (file)
@@ -195,7 +195,7 @@ Datum LWGEOM_npoints(PG_FUNCTION_ARGS)
        int npoints = 0;
 
        npoints = lwgeom_count_vertices(lwgeom);
-       lwgeom_release(lwgeom);
+       lwgeom_free(lwgeom);
 
        PG_FREE_IF_COPY(geom, 0);
        PG_RETURN_INT32(npoints);
@@ -210,7 +210,7 @@ Datum LWGEOM_nrings(PG_FUNCTION_ARGS)
        int nrings = 0;
 
        nrings = lwgeom_count_rings(lwgeom);
-       lwgeom_release(lwgeom);
+       lwgeom_free(lwgeom);
 
        PG_FREE_IF_COPY(geom, 0);
        PG_RETURN_INT32(nrings);
@@ -407,7 +407,7 @@ Datum LWGEOM_force_collection(PG_FUNCTION_ARGS)
 {
        GSERIALIZED *geom = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
        GSERIALIZED *result;
-       LWGEOM *lwgeoms[1];
+       LWGEOM **lwgeoms;
        LWGEOM *lwgeom;
        int srid;
        GBOX *bbox;
@@ -442,6 +442,7 @@ Datum LWGEOM_force_collection(PG_FUNCTION_ARGS)
                bbox = lwgeom->bbox;
                lwgeom->srid = SRID_UNKNOWN;
                lwgeom->bbox = NULL;
+               lwgeoms = palloc(sizeof(LWGEOM*));
                lwgeoms[0] = lwgeom;
                lwgeom = (LWGEOM *)lwcollection_construct(COLLECTIONTYPE,
                         srid, bbox, 1,
@@ -449,7 +450,7 @@ Datum LWGEOM_force_collection(PG_FUNCTION_ARGS)
        }
 
        result = geometry_serialize(lwgeom);
-       lwgeom_release(lwgeom);
+       lwgeom_free(lwgeom);
 
        PG_FREE_IF_COPY(geom, 0);
        PG_RETURN_POINTER(result);
@@ -991,7 +992,7 @@ Datum LWGEOM_longitude_shift(PG_FUNCTION_ARGS)
        ret = geometry_serialize(lwgeom);
 
        /* Release deserialized geometry */
-       lwgeom_release(lwgeom);
+       lwgeom_free(lwgeom);
 
        /* Release detoasted geometry */
        pfree(geom);
@@ -1476,8 +1477,8 @@ Datum LWGEOM_makeline(PG_FUNCTION_ARGS)
 
        PG_FREE_IF_COPY(pglwg1, 0);
        PG_FREE_IF_COPY(pglwg2, 1);
-       lwgeom_release((LWGEOM *)lwgeoms[0]);
-       lwgeom_release((LWGEOM *)lwgeoms[1]);
+       lwgeom_free(lwgeoms[0]);
+       lwgeom_free(lwgeoms[1]);
 
        PG_RETURN_POINTER(result);
 }
@@ -1530,14 +1531,16 @@ Datum LWGEOM_makepoly(PG_FUNCTION_ARGS)
        }
 
        outpoly = lwpoly_from_lwlines(shell, nholes, holes);
-
        POSTGIS_DEBUGF(3, "%s", lwgeom_summary((LWGEOM*)outpoly, 0));
-
        result = geometry_serialize((LWGEOM *)outpoly);
 
+       lwline_free((LWLINE*)shell);
        PG_FREE_IF_COPY(pglwg1, 0);
-       lwgeom_release((LWGEOM *)shell);
-       for (i=0; i<nholes; i++) lwgeom_release((LWGEOM *)holes[i]);
+
+       for (i=0; i<nholes; i++) 
+       {
+               lwline_free((LWLINE*)holes[i]);
+       }
 
        PG_RETURN_POINTER(result);
 }
@@ -1875,8 +1878,8 @@ Datum LWGEOM_noop(PG_FUNCTION_ARGS)
 
        out = geometry_serialize(lwgeom);
 
+       lwgeom_free(lwgeom);
        PG_FREE_IF_COPY(in, 0);
-       lwgeom_release(lwgeom);
 
        PG_RETURN_POINTER(out);
 }
@@ -2199,16 +2202,14 @@ Datum LWGEOM_removepoint(PG_FUNCTION_ARGS)
        }
 
        outline = lwline_removepoint(line, which);
-
-       result = geometry_serialize((LWGEOM *)outline);
-
        /* Release memory */
+       lwline_free(line);
        PG_FREE_IF_COPY(pglwg1, 0);
-       lwgeom_release((LWGEOM *)line);
-       lwgeom_release((LWGEOM *)outline);
 
-       PG_RETURN_POINTER(result);
+       result = geometry_serialize((LWGEOM *)outline);
+       lwline_free(outline);
 
+       PG_RETURN_POINTER(result);
 }
 
 PG_FUNCTION_INFO_V1(LWGEOM_setpoint_linestring);
@@ -2239,7 +2240,7 @@ Datum LWGEOM_setpoint_linestring(PG_FUNCTION_ARGS)
                PG_RETURN_NULL();
        }
        getPoint4d_p(lwpoint->point, 0, &newpoint);
-       lwgeom_release((LWGEOM *)lwpoint);
+       lwpoint_free(lwpoint);
        PG_FREE_IF_COPY(pglwg2, 2);
 
        lwg = lwgeom_from_gserialized(pglwg1);
@@ -2262,11 +2263,10 @@ Datum LWGEOM_setpoint_linestring(PG_FUNCTION_ARGS)
        result = geometry_serialize((LWGEOM *)line);
 
        /* Release memory */
+       lwline_free(line);
        pfree(pglwg1); /* we forced copy, POINARRAY is released now */
-       lwgeom_release((LWGEOM *)line);
 
        PG_RETURN_POINTER(result);
-
 }
 
 /* convert LWGEOM to ewkt (in TEXT format) */
index a9cdaf0233596982c70ba46c818141d8f0edb1c5..7749076417c1803b2217b252b2e35d46a80f478d 100644 (file)
@@ -45,7 +45,7 @@ Datum ST_AddMeasure(PG_FUNCTION_ARGS)
        else
                lwout = (LWGEOM*)lwmline_measured_from_lwmline((LWMLINE*)lwin, start_measure, end_measure);
 
-       lwgeom_release(lwin);
+       lwgeom_free(lwin);
 
        if ( lwout == NULL )
                PG_RETURN_NULL();
@@ -733,7 +733,7 @@ Datum LWGEOM_locate_between_m(PG_FUNCTION_ARGS)
        lwout = lwgeom_locate_between_m(lwin,
                                        start_measure, end_measure);
 
-       lwgeom_release(lwin);
+       lwgeom_free(lwin);
 
        if ( lwout == NULL )
        {
@@ -742,7 +742,7 @@ Datum LWGEOM_locate_between_m(PG_FUNCTION_ARGS)
        }
 
        gout = geometry_serialize(lwout);
-       lwgeom_release(lwout);
+       lwgeom_free(lwout);
 
        PG_RETURN_POINTER(gout);
 }
index 3f4a4dd61549379b49ac09fbba3c025e72c380c1..bc7dfc7e7fd49d516f3464c7e79081724007c555 100644 (file)
@@ -990,25 +990,23 @@ Datum convexhull(PG_FUNCTION_ARGS)
        }
 
        g3 = (GEOSGeometry *)GEOSConvexHull(g1);
+       GEOSGeom_destroy(g1);
 
        if (g3 == NULL)
        {
-               GEOSGeom_destroy(g1);
                lwerror("GEOSConvexHull: %s", lwgeom_geos_errmsg);
                PG_RETURN_NULL(); /* never get here */
        }
 
-
        POSTGIS_DEBUGF(3, "result: %s", GEOSGeomToWKT(g3));
 
        GEOSSetSRID(g3, srid);
 
        lwout = GEOS2LWGEOM(g3, gserialized_has_z(geom1));
+       GEOSGeom_destroy(g3);
 
        if (lwout == NULL)
        {
-               GEOSGeom_destroy(g1);
-               GEOSGeom_destroy(g3);
                elog(ERROR,"convexhull() failed to convert GEOS geometry to LWGEOM");
                PG_RETURN_NULL(); /* never get here */
        }
@@ -1022,18 +1020,13 @@ Datum convexhull(PG_FUNCTION_ARGS)
        }
 
        result = geometry_serialize(lwout);
+       lwgeom_free(lwout);
 
        if (result == NULL)
        {
-               GEOSGeom_destroy(g1);
-               GEOSGeom_destroy(g3);
                elog(ERROR,"GEOS convexhull() threw an error (result postgis geometry formation)!");
                PG_RETURN_NULL(); /* never get here */
        }
-       lwgeom_release(lwout);
-       GEOSGeom_destroy(g1);
-       GEOSGeom_destroy(g3);
-
 
        PG_FREE_IF_COPY(geom1, 0);
        PG_RETURN_POINTER(result);
@@ -1064,30 +1057,26 @@ Datum topologypreservesimplify(PG_FUNCTION_ARGS)
        }
 
        g3 = GEOSTopologyPreserveSimplify(g1,tolerance);
+       GEOSGeom_destroy(g1);
 
        if (g3 == NULL)
        {
-               GEOSGeom_destroy(g1);
                lwerror("GEOSTopologyPreserveSimplify: %s", lwgeom_geos_errmsg);
                PG_RETURN_NULL(); /* never get here */
        }
 
-
        POSTGIS_DEBUGF(3, "result: %s", GEOSGeomToWKT(g3));
 
        GEOSSetSRID(g3, gserialized_get_srid(geom1));
 
        result = GEOS2POSTGIS(g3, gserialized_has_z(geom1));
+       GEOSGeom_destroy(g3);
 
        if (result == NULL)
        {
-               GEOSGeom_destroy(g1);
-               GEOSGeom_destroy(g3);
                elog(ERROR,"GEOS topologypreservesimplify() threw an error (result postgis geometry formation)!");
                PG_RETURN_NULL(); /* never get here */
        }
-       GEOSGeom_destroy(g1);
-       GEOSGeom_destroy(g3);
 
        PG_FREE_IF_COPY(geom1, 0);
        PG_RETURN_POINTER(result);
@@ -1259,8 +1248,8 @@ Datum buffer(PG_FUNCTION_ARGS)
 
 #if POSTGIS_GEOS_VERSION >= 32
 
-       g3 = GEOSBufferWithStyle(g1, size, quadsegs,
-                                endCapStyle, joinStyle, mitreLimit);
+       g3 = GEOSBufferWithStyle(g1, size, quadsegs, endCapStyle, joinStyle, mitreLimit);
+       GEOSGeom_destroy(g1);
 
 #else /* POSTGIS_GEOS_VERSION < 32 */
 
@@ -1276,12 +1265,12 @@ Datum buffer(PG_FUNCTION_ARGS)
        }
 
        g3 = GEOSBuffer(g1,size,quadsegs);
+       GEOSGeom_destroy(g1);
 
 #endif /* POSTGIS_GEOS_VERSION < 32 */
 
        if (g3 == NULL)
        {
-               GEOSGeom_destroy(g1);
                lwerror("GEOSBuffer: %s", lwgeom_geos_errmsg);
                PG_RETURN_NULL(); /* never get here */
        }
@@ -1291,19 +1280,15 @@ Datum buffer(PG_FUNCTION_ARGS)
        GEOSSetSRID(g3, gserialized_get_srid(geom1));
 
        result = GEOS2POSTGIS(g3, gserialized_has_z(geom1));
+       GEOSGeom_destroy(g3);
 
        if (result == NULL)
        {
-               GEOSGeom_destroy(g1);
-               GEOSGeom_destroy(g3);
                elog(ERROR,"GEOS buffer() threw an error (result postgis geometry formation)!");
                PG_RETURN_NULL(); /* never get here */
        }
-       GEOSGeom_destroy(g1);
-       GEOSGeom_destroy(g3);
 
        PG_FREE_IF_COPY(geom1, 0);
-
        PG_RETURN_POINTER(result);
 }
 
@@ -1714,20 +1699,20 @@ Datum isvalid(PG_FUNCTION_ARGS)
                lwerror("unable to deserialize input");
        }
        g1 = LWGEOM2GEOS(lwgeom);
+       lwgeom_free(lwgeom);
+       
        if ( ! g1 )
        {
                /* should we drop the following
                 * notice now that we have ST_isValidReason ?
                 */
                lwnotice("%s", lwgeom_geos_errmsg);
-               lwgeom_release(lwgeom);
                PG_RETURN_BOOL(FALSE);
        }
-       lwgeom_release(lwgeom);
 
        result = GEOSisValid(g1);
-
        GEOSGeom_destroy(g1);
+
        if (result == 2)
        {
                elog(ERROR,"GEOS isvalid() threw an error!");
@@ -1735,7 +1720,6 @@ Datum isvalid(PG_FUNCTION_ARGS)
        }
 
        PG_FREE_IF_COPY(geom1, 0);
-
        PG_RETURN_BOOL(result);
 }
 
@@ -2059,10 +2043,10 @@ Datum contains(PG_FUNCTION_ARGS)
                        elog(ERROR,"Type isn't poly or multipoly!");
                        PG_RETURN_NULL();
                }
+               lwgeom_free(lwgeom);
+               lwpoint_free(point);
                PG_FREE_IF_COPY(geom1, 0);
                PG_FREE_IF_COPY(geom2, 1);
-               lwgeom_release((LWGEOM *)lwgeom);
-               lwgeom_release((LWGEOM *)point);
                if ( result == 1 ) /* completely inside */
                {
                        PG_RETURN_BOOL(TRUE);
@@ -2294,10 +2278,10 @@ Datum covers(PG_FUNCTION_ARGS)
                        PG_RETURN_NULL();
                }
 
+               lwgeom_free(lwgeom);
+               lwpoint_free(point);
                PG_FREE_IF_COPY(geom1, 0);
                PG_FREE_IF_COPY(geom2, 1);
-               lwgeom_release((LWGEOM *)lwgeom);
-               lwgeom_release((LWGEOM *)point);
                if ( result != -1 ) /* not outside */
                {
                        PG_RETURN_BOOL(TRUE);
@@ -2451,10 +2435,10 @@ Datum coveredby(PG_FUNCTION_ARGS)
                        PG_RETURN_NULL();
                }
 
+               lwgeom_free(lwgeom);
+               lwpoint_free(point);
                PG_FREE_IF_COPY(geom1, 0);
                PG_FREE_IF_COPY(geom2, 1);
-               lwgeom_release((LWGEOM *)lwgeom);
-               lwgeom_release((LWGEOM *)point);
                if ( result != -1 ) /* not outside */
                {
                        PG_RETURN_BOOL(TRUE);
@@ -2528,7 +2512,7 @@ Datum crosses(PG_FUNCTION_ARGS)
         * Do the test IFF BOUNDING BOX AVAILABLE.
         */
        if ( gserialized_get_gbox_p(geom1, &box1) &&
-               gserialized_get_gbox_p(geom2, &box2) )
+            gserialized_get_gbox_p(geom2, &box2) )
        {
                if ( gbox_overlaps_2d(&box1, &box2) == LW_FALSE )
                {
@@ -2658,10 +2642,10 @@ Datum intersects(PG_FUNCTION_ARGS)
                        PG_RETURN_NULL();
                }
 
+               lwgeom_free(lwgeom);
+               lwpoint_free(point);
                PG_FREE_IF_COPY(geom1, 0);
                PG_FREE_IF_COPY(geom2, 1);
-               lwgeom_release((LWGEOM *)lwgeom);
-               lwgeom_release((LWGEOM *)point);
                if ( result != -1 ) /* not outside */
                {
                        PG_RETURN_BOOL(TRUE);
@@ -3193,6 +3177,7 @@ GEOS2POSTGIS(GEOSGeom geom, char want3d)
        }
 
        result = geometry_serialize(lwgeom);
+       lwgeom_free(lwgeom);
 
        return result;
 }
@@ -3211,7 +3196,7 @@ POSTGIS2GEOS(GSERIALIZED *pglwgeom)
                return NULL;
        }
        ret = LWGEOM2GEOS(lwgeom);
-       lwgeom_release(lwgeom);
+       lwgeom_free(lwgeom);
        if ( ! ret )
        {
                /* lwerror("POSTGIS2GEOS conversion failed"); */
@@ -3411,17 +3396,15 @@ Datum ST_BuildArea(PG_FUNCTION_ARGS)
        lwgeom_in = lwgeom_from_gserialized(geom);
 
        lwgeom_out = lwgeom_buildarea(lwgeom_in);
+       lwgeom_free(lwgeom_in) ;
+       PG_FREE_IF_COPY(geom, 0);
+       
        if ( ! lwgeom_out ) {
-               lwgeom_free(lwgeom_in) ;
-               PG_FREE_IF_COPY(geom, 0);
                PG_RETURN_NULL();
        }
 
        result = geometry_serialize(lwgeom_out) ;
-
        lwgeom_free(lwgeom_out) ;
-       lwgeom_free(lwgeom_in) ;
-       PG_FREE_IF_COPY(geom, 0);
 
        PG_RETURN_POINTER(result);
 }
@@ -3450,19 +3433,18 @@ Datum ST_Snap(PG_FUNCTION_ARGS)
        geom2 = (GSERIALIZED *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
        tolerance = PG_GETARG_FLOAT8(2);
 
-       lwgeom1 = lwgeom_from_gserialized(geom1) ;
-       lwgeom2 = lwgeom_from_gserialized(geom2) ;
+       lwgeom1 = lwgeom_from_gserialized(geom1);
+       lwgeom2 = lwgeom_from_gserialized(geom2);
 
        lwresult = lwgeom_snap(lwgeom1, lwgeom2, tolerance);
-       result = geometry_serialize(lwresult);
-
        lwgeom_free(lwgeom1);
        lwgeom_free(lwgeom2);
-       lwgeom_free(lwresult);
-
        PG_FREE_IF_COPY(geom1, 0);
        PG_FREE_IF_COPY(geom2, 1);
 
+       result = geometry_serialize(lwresult);
+       lwgeom_free(lwresult);
+
        PG_RETURN_POINTER(result);
 
 #endif /* POSTGIS_GEOS_VERSION >= 33 */
@@ -3509,20 +3491,20 @@ Datum ST_Split(PG_FUNCTION_ARGS)
        error_if_srid_mismatch(lwgeom_in->srid, lwblade_in->srid);
 
        lwgeom_out = lwgeom_split(lwgeom_in, lwblade_in);
+       lwgeom_free(lwgeom_in);
+       lwgeom_free(lwblade_in);
+       PG_FREE_IF_COPY(in, 0);
+       PG_FREE_IF_COPY(blade_in, 1);
+       
        if ( ! lwgeom_out )
        {
-               PG_FREE_IF_COPY(in, 0);
-               PG_FREE_IF_COPY(blade_in, 1);
                PG_RETURN_NULL();
        }
 
        out = geometry_serialize(lwgeom_out);
-
-       PG_FREE_IF_COPY(in, 0);
-       PG_FREE_IF_COPY(blade_in, 1);
+       lwgeom_free(lwgeom_out);
 
        PG_RETURN_POINTER(out);
-
 }
 
 /**********************************************************************
@@ -3561,16 +3543,18 @@ Datum ST_SharedPaths(PG_FUNCTION_ARGS)
        g2 = lwgeom_from_gserialized(geom2);
 
        lwgeom_out = lwgeom_sharedpaths(g1, g2);
+       lwgeom_free(g1);
+       lwgeom_free(g2);
+       PG_FREE_IF_COPY(geom1, 0);
+       PG_FREE_IF_COPY(geom2, 1);
+
        if ( ! lwgeom_out )
        {
-               PG_FREE_IF_COPY(geom1, 0);
-               PG_FREE_IF_COPY(geom2, 1);
                PG_RETURN_NULL();
        }
 
        out = geometry_serialize(lwgeom_out);
-       PG_FREE_IF_COPY(geom1, 0);
-       PG_FREE_IF_COPY(geom2, 1);
+       lwgeom_free(lwgeom_out);
 
        PG_RETURN_POINTER(out);
 
@@ -3606,14 +3590,16 @@ Datum ST_Node(PG_FUNCTION_ARGS)
        g1 = lwgeom_from_gserialized(geom1);
 
        lwgeom_out = lwgeom_node(g1);
+       lwgeom_free(g1);
+       PG_FREE_IF_COPY(geom1, 0);
+
        if ( ! lwgeom_out )
        {
-               PG_FREE_IF_COPY(geom1, 0);
                PG_RETURN_NULL();
        }
 
        out = geometry_serialize(lwgeom_out);
-       PG_FREE_IF_COPY(geom1, 0);
+       lwgeom_free(lwgeom_out);
 
        PG_RETURN_POINTER(out);
 
index 5d0b431c64537f9e81395504a4ceb66d2642d7d1..3a9219d0aafe586d5f1484a61495cc3854c88bbb 100644 (file)
@@ -277,7 +277,7 @@ Datum LWGEOM_geometryn_collection(PG_FUNCTION_ARGS)
 
        result = geometry_serialize(subgeom);
 
-       lwgeom_release((LWGEOM *)coll);
+       lwcollection_free(coll);
        PG_FREE_IF_COPY(geom, 0);
 
        PG_RETURN_POINTER(result);
@@ -470,7 +470,7 @@ Datum LWGEOM_interiorringn_polygon(PG_FUNCTION_ARGS)
        lwgeom = lwgeom_from_gserialized(geom);
        if( lwgeom_is_empty(lwgeom) )
        {
-               lwgeom_free((LWGEOM *)poly);
+               lwpoly_free(poly);
                PG_FREE_IF_COPY(geom, 0);
                PG_RETURN_NULL();
        }
@@ -482,7 +482,7 @@ Datum LWGEOM_interiorringn_polygon(PG_FUNCTION_ARGS)
                /* Ok, now we have a polygon. Let's see if it has enough holes */
                if ( wanted_index >= poly->nrings )
                {
-                       lwgeom_free((LWGEOM *)poly);
+                       lwpoly_free(poly);
                        PG_FREE_IF_COPY(geom, 0);
                        PG_RETURN_NULL();
                }
@@ -501,8 +501,8 @@ Datum LWGEOM_interiorringn_polygon(PG_FUNCTION_ARGS)
 
 
                result = geometry_serialize((LWGEOM *)line);
-               lwpoly_free(poly);
                lwline_release(line);
+               lwpoly_free(poly);
        }
        else
        {
@@ -516,7 +516,7 @@ Datum LWGEOM_interiorringn_polygon(PG_FUNCTION_ARGS)
                }
 
                result = geometry_serialize(curvepoly->rings[wanted_index]);
-               lwgeom_free((LWGEOM *)curvepoly);
+               lwgeom_free((LWGEOM*)curvepoly);
        }
 
        PG_FREE_IF_COPY(geom, 0);
index bb9942f5adde2ed0994130a2a680c15a86a2c859..dff6810cb88c87f3dccb6f7b9cf3cab5fc42f90b 100644 (file)
@@ -201,9 +201,7 @@ void freeTree(RTREE_NODE *root)
        lwfree(root->interval);
        if (root->segment)
        {
-               lwfree(root->segment->points->serialized_pointlist);
-               lwfree(root->segment->points);
-               lwgeom_release((LWGEOM *)root->segment);
+               lwline_free(root->segment);
        }
        lwfree(root);
 }
@@ -346,7 +344,6 @@ PG_FUNCTION_INFO_V1(LWGEOM_polygon_index);
 Datum LWGEOM_polygon_index(PG_FUNCTION_ARGS)
 {
        GSERIALIZED *igeom, *result;
-       LWGEOM *geom;
        LWPOLY *poly;
        LWMLINE *mline;
        RTREE_NODE *root;
@@ -360,14 +357,12 @@ Datum LWGEOM_polygon_index(PG_FUNCTION_ARGS)
        result = NULL;
        igeom = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
        yval = PG_GETARG_FLOAT8(1);
-       geom = lwgeom_from_gserialized(igeom);
-       if (geom->type != POLYGONTYPE)
+       if ( gserialized_get_type(igeom) != POLYGONTYPE )
        {
-               lwgeom_release(geom);
                PG_FREE_IF_COPY(igeom, 0);
                PG_RETURN_NULL();
        }
-       poly = (LWPOLY *)geom;
+       poly = lwgeom_as_lwpoly(lwgeom_from_gserialized(igeom));
        root = createTree(poly->rings[0]);
 
        mline = findLineSegments(root, yval);
@@ -387,9 +382,9 @@ Datum LWGEOM_polygon_index(PG_FUNCTION_ARGS)
 
        lwfree(root);
 
+       lwpoly_free(poly);
+       lwmline_free(mline);
        PG_FREE_IF_COPY(igeom, 0);
-       lwgeom_release((LWGEOM *)poly);
-       lwgeom_release((LWGEOM *)mline);
        PG_RETURN_POINTER(result);
 
 }
index dfac6cb1037039f23d03d585f87a49dd1b8c3f1c..ddbc7cb149b120a790713006c2b090a91885f0fe 100644 (file)
@@ -326,7 +326,8 @@ Datum LWGEOM_length2d_ellipsoid(PG_FUNCTION_ARGS)
        SPHEROID *sphere = (SPHEROID *) PG_GETARG_POINTER(1);
        LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
        double dist = lwgeom_length_spheroid(lwgeom, sphere);
-       lwgeom_release(lwgeom);
+       lwgeom_free(lwgeom);
+       PG_FREE_IF_COPY(geom, 0);
        PG_RETURN_FLOAT8(dist);
 }
 
@@ -357,6 +358,8 @@ Datum LWGEOM_length_ellipsoid_linestring(PG_FUNCTION_ARGS)
        }
 
        length = lwgeom_length_spheroid(lwgeom, sphere);
+       lwgeom_free(lwgeom);
+       PG_FREE_IF_COPY(geom, 0);
 
        /* Something went wrong... */
        if ( length < 0.0 )
@@ -365,10 +368,7 @@ Datum LWGEOM_length_ellipsoid_linestring(PG_FUNCTION_ARGS)
                PG_RETURN_NULL();
        }
 
-       /* Clean up, but not all the way to the point arrays */
-       lwgeom_release(lwgeom);
-
-       PG_FREE_IF_COPY(geom, 0);
+       /* Clean up */
        PG_RETURN_FLOAT8(length);
 }
 
index 61de2193771e16b47f9769956766822f889142b0..37d2d57b32330ef48fa4eee54051a66f5d8f53af 100644 (file)
@@ -78,8 +78,8 @@ Datum LWGEOM_curve_segmentize(PG_FUNCTION_ARGS)
        ogeom = lwgeom_segmentize(igeom, perQuad);
        if (ogeom == NULL) PG_RETURN_NULL();
        ret = geometry_serialize(ogeom);
-       lwgeom_release(igeom);
-       lwgeom_release(ogeom);
+       lwgeom_free(igeom);
+       lwgeom_free(ogeom);
        PG_FREE_IF_COPY(geom, 0);
        PG_RETURN_POINTER(ret);
 }
@@ -95,15 +95,14 @@ Datum LWGEOM_line_desegmentize(PG_FUNCTION_ARGS)
 
        igeom = lwgeom_from_gserialized(geom);
        ogeom = lwgeom_desegmentize(igeom);
+       lwgeom_free(igeom);
+       PG_FREE_IF_COPY(geom, 0);
+
        if (ogeom == NULL)
-       {
-               lwgeom_release(igeom);
                PG_RETURN_NULL();
-       }
+
        ret = geometry_serialize(ogeom);
-       lwgeom_release(igeom);
-       lwgeom_release(ogeom);
-       PG_FREE_IF_COPY(geom, 0);
+       lwgeom_free(ogeom);
        PG_RETURN_POINTER(ret);
 }