]> granicus.if.org Git - postgis/commitdiff
expected in-transaction memory release for btree operators
authorSandro Santilli <strk@keybit.net>
Thu, 20 Nov 2003 15:34:02 +0000 (15:34 +0000)
committerSandro Santilli <strk@keybit.net>
Thu, 20 Nov 2003 15:34:02 +0000 (15:34 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@376 b70326c6-7e19-0410-871a-916f4a2858ee

postgis_ops.c

index e1e9fcfa7ad018cc34e269df7cc98ae1ca7fbfa5..ec08e46c2e2480b0f5ee071fce0d012ebd80f02e 100644 (file)
@@ -11,6 +11,9 @@
  * 
  **********************************************************************
  * $Log$
+ * Revision 1.12  2003/11/20 15:34:02  strk
+ * expected in-transaction memory release for btree operators
+ *
  * Revision 1.11  2003/11/19 15:26:57  strk
  * Added geometry_le, geometry_ge, geometry_cmp functions,
  * modified geometry_lt, geometry_gt, geometry_eq to be consistent.
@@ -303,6 +306,8 @@ Datum geometry_lt(PG_FUNCTION_ARGS)
        GEOMETRY *geom1 = (GEOMETRY *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
        GEOMETRY *geom2 = (GEOMETRY *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
 
+       //elog(NOTICE, "geometry_lt called");
+
        if (geom1->SRID != geom2->SRID)
        {
                elog(ERROR,
@@ -349,8 +354,12 @@ Datum geometry_le(PG_FUNCTION_ARGS)
        GEOMETRY *geom1 = (GEOMETRY *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
        GEOMETRY *geom2 = (GEOMETRY *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
 
+       //elog(NOTICE, "geometry_le called");
+
        if (geom1->SRID != geom2->SRID)
        {
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                elog(ERROR,
                        "Operation on two GEOMETRIES with different SRIDs\n");
                PG_RETURN_NULL();
@@ -358,40 +367,79 @@ Datum geometry_le(PG_FUNCTION_ARGS)
 
        if  ( ! FPeq(geom1->bvol.LLB.x , geom2->bvol.LLB.x) ) {
                if  (geom1->bvol.LLB.x < geom2->bvol.LLB.x)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_BOOL(TRUE);
+               }
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                PG_RETURN_BOOL(FALSE);
        }
 
        if  ( ! FPeq(geom1->bvol.LLB.y , geom2->bvol.LLB.y) ) {
                if  (geom1->bvol.LLB.y < geom2->bvol.LLB.y)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_BOOL(TRUE);
+               }
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                PG_RETURN_BOOL(FALSE);
        }
 
        if  ( ! FPeq(geom1->bvol.LLB.z , geom2->bvol.LLB.z) ) {
                if  (geom1->bvol.LLB.z < geom2->bvol.LLB.z)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_BOOL(TRUE);
+               }
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                PG_RETURN_BOOL(FALSE);
        }
 
        if  ( ! FPeq(geom1->bvol.URT.x , geom2->bvol.URT.x) ) {
                if  (geom1->bvol.URT.x < geom2->bvol.URT.x)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_BOOL(TRUE);
+               }
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                PG_RETURN_BOOL(FALSE);
        }
 
        if  ( ! FPeq(geom1->bvol.URT.y , geom2->bvol.URT.y) ) {
                if  (geom1->bvol.URT.y < geom2->bvol.URT.y)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_BOOL(TRUE);
+               }
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                PG_RETURN_BOOL(FALSE);
        }
 
        if  ( ! FPeq(geom1->bvol.URT.z , geom2->bvol.URT.z) ) {
                if  (geom1->bvol.URT.z < geom2->bvol.URT.z)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_BOOL(TRUE);
+               }
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                PG_RETURN_BOOL(FALSE);
        }
 
+       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
+
        PG_RETURN_BOOL(TRUE);
 }
 
@@ -401,30 +449,61 @@ Datum geometry_eq(PG_FUNCTION_ARGS)
        GEOMETRY *geom1 = (GEOMETRY *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
        GEOMETRY *geom2 = (GEOMETRY *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
 
+       //elog(NOTICE, "geometry_eq called");
+
        if (geom1->SRID != geom2->SRID)
        {
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                elog(ERROR,
                        "Operation on two GEOMETRIES with different SRIDs\n");
                PG_RETURN_NULL();
        }
 
        if  ( ! FPeq(geom1->bvol.LLB.x , geom2->bvol.LLB.x) ) 
+       {
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                PG_RETURN_BOOL(FALSE);
+       }
 
        if  ( ! FPeq(geom1->bvol.LLB.y , geom2->bvol.LLB.y) ) 
+       {
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                PG_RETURN_BOOL(FALSE);
+       }
 
        if  ( ! FPeq(geom1->bvol.LLB.z , geom2->bvol.LLB.z) ) 
+       {
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                PG_RETURN_BOOL(FALSE);
+       }
 
        if  ( ! FPeq(geom1->bvol.URT.x , geom2->bvol.URT.x) ) 
+       {
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                PG_RETURN_BOOL(FALSE);
+       }
 
        if  ( ! FPeq(geom1->bvol.URT.y , geom2->bvol.URT.y) )
+       {
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                PG_RETURN_BOOL(FALSE);
+       }
 
        if  ( ! FPeq(geom1->bvol.URT.z , geom2->bvol.URT.z) ) 
+       {
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                PG_RETURN_BOOL(FALSE);
+       }
+
+       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
 
        PG_RETURN_BOOL(TRUE);
 }
@@ -435,8 +514,12 @@ Datum geometry_ge(PG_FUNCTION_ARGS)
        GEOMETRY *geom1 = (GEOMETRY *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
        GEOMETRY *geom2 = (GEOMETRY *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
 
+       //elog(NOTICE, "geometry_ge called");
+
        if (geom1->SRID != geom2->SRID)
        {
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                elog(ERROR,
                        "Operation on two GEOMETRIES with different SRIDs\n");
                PG_RETURN_NULL();
@@ -444,40 +527,67 @@ Datum geometry_ge(PG_FUNCTION_ARGS)
 
        if  ( ! FPeq(geom1->bvol.LLB.x , geom2->bvol.LLB.x) ) {
                if  (geom1->bvol.LLB.x > geom2->bvol.LLB.x)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_BOOL(TRUE);
+               }
                PG_RETURN_BOOL(FALSE);
        }
 
        if  ( ! FPeq(geom1->bvol.LLB.y , geom2->bvol.LLB.y) ) {
                if  (geom1->bvol.LLB.y > geom2->bvol.LLB.y)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_BOOL(TRUE);
+               }
                PG_RETURN_BOOL(FALSE);
        }
 
        if  ( ! FPeq(geom1->bvol.LLB.z , geom2->bvol.LLB.z) ) {
                if  (geom1->bvol.LLB.z > geom2->bvol.LLB.z)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_BOOL(TRUE);
+               }
                PG_RETURN_BOOL(FALSE);
        }
 
        if  ( ! FPeq(geom1->bvol.URT.x , geom2->bvol.URT.x) ) {
                if  (geom1->bvol.URT.x > geom2->bvol.URT.x)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_BOOL(TRUE);
+               }
                PG_RETURN_BOOL(FALSE);
        }
 
        if  ( ! FPeq(geom1->bvol.URT.y , geom2->bvol.URT.y) ) {
                if  (geom1->bvol.URT.y > geom2->bvol.URT.y)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_BOOL(TRUE);
+               }
                PG_RETURN_BOOL(FALSE);
        }
 
        if  ( ! FPeq(geom1->bvol.URT.z , geom2->bvol.URT.z) ) {
                if  (geom1->bvol.URT.z > geom2->bvol.URT.z)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_BOOL(TRUE);
+               }
                PG_RETURN_BOOL(FALSE);
        }
 
+       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
+
        PG_RETURN_BOOL(TRUE);
 }
 
@@ -487,8 +597,14 @@ Datum geometry_gt(PG_FUNCTION_ARGS)
        GEOMETRY *geom1 = (GEOMETRY *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
        GEOMETRY *geom2 = (GEOMETRY *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
 
+       //elog(NOTICE, "geometry_gt called");
+
        if (geom1->SRID != geom2->SRID)
        {
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 )
+                       pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 )
+                       pfree(geom2);
                elog(ERROR,
                        "Operation on two GEOMETRIES with different SRIDs\n");
                PG_RETURN_NULL();
@@ -496,34 +612,61 @@ Datum geometry_gt(PG_FUNCTION_ARGS)
 
        if  ( ! FPeq(geom1->bvol.LLB.x , geom2->bvol.LLB.x) ) {
                if  (geom1->bvol.LLB.x > geom2->bvol.LLB.x)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_BOOL(TRUE);
+               }
        }
 
        if  ( ! FPeq(geom1->bvol.LLB.y , geom2->bvol.LLB.y) ) {
                if  (geom1->bvol.LLB.y > geom2->bvol.LLB.y)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_BOOL(TRUE);
+               }
        }
 
        if  ( ! FPeq(geom1->bvol.LLB.z , geom2->bvol.LLB.z) ) {
                if  (geom1->bvol.LLB.z > geom2->bvol.LLB.z)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_BOOL(TRUE);
+               }
        }
 
        if  ( ! FPeq(geom1->bvol.URT.x , geom2->bvol.URT.x) ) {
                if  (geom1->bvol.URT.x > geom2->bvol.URT.x)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_BOOL(TRUE);
+               }
        }
 
        if  ( ! FPeq(geom1->bvol.URT.y , geom2->bvol.URT.y) ) {
                if  (geom1->bvol.URT.y > geom2->bvol.URT.y)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_BOOL(TRUE);
+               }
        }
 
        if  ( ! FPeq(geom1->bvol.URT.z , geom2->bvol.URT.z) ) {
                if  (geom1->bvol.URT.z > geom2->bvol.URT.z)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_BOOL(TRUE);
+               }
        }
 
+       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
+
        PG_RETURN_BOOL(FALSE);
 }
 
@@ -533,8 +676,14 @@ Datum geometry_cmp(PG_FUNCTION_ARGS)
        GEOMETRY *geom1 = (GEOMETRY *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
        GEOMETRY *geom2 = (GEOMETRY *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
 
+       //elog(NOTICE, "geometry_cmp called");
+
        if (geom1->SRID != geom2->SRID)
        {
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 )
+                       pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 )
+                       pfree(geom2);
                elog(ERROR,
                        "Operation on two GEOMETRIES with different SRIDs\n");
                PG_RETURN_NULL();
@@ -542,48 +691,83 @@ Datum geometry_cmp(PG_FUNCTION_ARGS)
 
        if  ( ! FPeq(geom1->bvol.LLB.x , geom2->bvol.LLB.x) ) {
                if  (geom1->bvol.LLB.x < geom2->bvol.LLB.x)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_INT32(-1);
+               }
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                PG_RETURN_INT32(1);
        }
 
        if  ( ! FPeq(geom1->bvol.LLB.y , geom2->bvol.LLB.y) ) {
                if  (geom1->bvol.LLB.y < geom2->bvol.LLB.y)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_INT32(-1);
+               }
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                PG_RETURN_INT32(1);
        }
 
        if  ( ! FPeq(geom1->bvol.LLB.z , geom2->bvol.LLB.z) ) {
                if  (geom1->bvol.LLB.z < geom2->bvol.LLB.z)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_INT32(-1);
+               }
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                PG_RETURN_INT32(1);
        }
 
        if  ( ! FPeq(geom1->bvol.URT.x , geom2->bvol.URT.x) ) {
                if  (geom1->bvol.URT.x < geom2->bvol.URT.x)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_INT32(-1);
+               }
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                PG_RETURN_INT32(1);
        }
 
        if  ( ! FPeq(geom1->bvol.URT.y , geom2->bvol.URT.y) ) {
                if  (geom1->bvol.URT.y < geom2->bvol.URT.y)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_INT32(-1);
+               }
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                PG_RETURN_INT32(1);
        }
 
        if  ( ! FPeq(geom1->bvol.URT.z , geom2->bvol.URT.z) ) {
                if  (geom1->bvol.URT.z < geom2->bvol.URT.z)
+               {
+                       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+                       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                        PG_RETURN_INT32(-1);
+               }
+               if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+               if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
                PG_RETURN_INT32(1);
        }
 
+       if ( (Pointer *)PG_GETARG_DATUM(0) != (Pointer *)geom1 ) pfree(geom1);
+       if ( (Pointer *)PG_GETARG_DATUM(1) != (Pointer *)geom2 ) pfree(geom2);
+
        PG_RETURN_INT32(0);
 }
 
 
-
-
-
-
 //order of points in a ring IS important
 //order of rings is also important
 bool is_same_polygon(POLYGON3D *poly1, POLYGON3D       *poly2)