]> granicus.if.org Git - postgis/commitdiff
More small memory leaks removed.
authorPaul Ramsey <pramsey@cleverelephant.ca>
Thu, 25 Sep 2008 19:18:37 +0000 (19:18 +0000)
committerPaul Ramsey <pramsey@cleverelephant.ca>
Thu, 25 Sep 2008 19:18:37 +0000 (19:18 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@3009 b70326c6-7e19-0410-871a-916f4a2858ee

lwgeom/lwgeom_rtree.c
lwgeom/lwgeom_rtree.h

index 5fe62ae964032d5e3693a0f7daef50d357bc6298..8589540a3d87ef2ec720c112e0266648ac14b40a 100644 (file)
@@ -16,7 +16,6 @@
 
 
 Datum LWGEOM_polygon_index(PG_FUNCTION_ARGS);
-RTREE_POLY_CACHE *createNewCache(LWPOLY *poly, uchar *serializedPoly);
 
 
 /*
@@ -76,7 +75,7 @@ RTREE_NODE *createTree(POINTARRAY *pointArray)
         }
 
         root = nodes[0];
-
+        lwfree(nodes);
         LWDEBUGF(3, "createTree returning %p", root);
 
         return root;
@@ -208,6 +207,24 @@ void freeTree(RTREE_NODE *root)
         lwfree(root);
 }
 
+
+/*
+ * Free the cache object and all the sub-objects properly.
+ */
+void freeCache(RTREE_POLY_CACHE *cache)
+{
+    int i;
+    LWDEBUGF(2, "freeCache called for %p", cache);
+    for(i = 0; i < cache->ringCount; i++)
+    { 
+           freeTree(cache->ringIndices[i]);
+    }
+    lwfree(cache->ringIndices);
+    lwfree(cache->poly);
+    lwfree(cache);
+}
+
 /*
  * Retrieves a collection of line segments given the root and crossing value.
  * The collection is a multilinestring consisting of two point lines 
@@ -400,58 +417,38 @@ RTREE_POLY_CACHE *createNewCache(LWPOLY *poly, uchar *serializedPoly)
 RTREE_POLY_CACHE *retrieveCache(LWPOLY *poly, uchar *serializedPoly, 
                 RTREE_POLY_CACHE *currentCache)
 {
-        int i, length;
-
-        LWDEBUGF(2, "retrieveCache called with %p %p %p", poly, serializedPoly, currentCache);
-
-        if(!currentCache)
-        {
-                LWDEBUG(3, "No existing cache, create one.");
-
-                return createNewCache(poly, serializedPoly);
-        }
-        if(!(currentCache->poly))
-        {
-                LWDEBUG(3, "Cache contains no polygon, creating new cache.");
-
-                return createNewCache(poly, serializedPoly);
-        }
-
-        length = lwgeom_size_poly(serializedPoly);
-
-        if(lwgeom_size_poly(currentCache->poly) != length)
-        {
-                LWDEBUG(3, "Polygon size mismatch, creating new cache.");
-                for(i = 0; i < currentCache->ringCount; i++)
-                {
-                    freeTree(currentCache->ringIndices[i]);
-                }
-                lwfree(currentCache->ringIndices);
-                lwfree(currentCache->poly);
-                lwfree(currentCache);
-                return createNewCache(poly, serializedPoly);
-        }
-        for(i = 0; i < length; i++) 
-        {
-                uchar a = serializedPoly[i];
-                uchar b = currentCache->poly[i];
-                if(a != b) 
-                {
-                        LWDEBUGF(3, "Polygon mismatch, creating new cache. %c, %c", a, b);
-
-                        for(i = 0; i < currentCache->ringCount; i++)
-                        { 
-                                       freeTree(currentCache->ringIndices[i]);
-                        }
-                        lwfree(currentCache->ringIndices);
-                        lwfree(currentCache->poly);
-                        lwfree(currentCache);
-                        return createNewCache(poly, serializedPoly);
-                }
-        }
-
-        LWDEBUGF(3, "Polygon match, retaining current cache, %p.", currentCache);
-
-        return currentCache;
+    int length;
+
+    LWDEBUGF(2, "retrieveCache called with %p %p %p", poly, serializedPoly, currentCache);
+
+    if(!currentCache)
+    {
+        LWDEBUG(3, "No existing cache, create one.");
+        return createNewCache(poly, serializedPoly);
+    }
+    if(!(currentCache->poly))
+    {
+        LWDEBUG(3, "Cache contains no polygon, creating new cache.");
+        return createNewCache(poly, serializedPoly);
+    }
+
+    length = lwgeom_size_poly(serializedPoly);
+
+    if(lwgeom_size_poly(currentCache->poly) != length)
+    {
+        LWDEBUG(3, "Polygon size mismatch, creating new cache.");
+        freeCache(currentCache);
+        return createNewCache(poly, serializedPoly);
+    }
+    if( memcmp(serializedPoly, currentCache->poly, length) ) 
+    {
+        LWDEBUGF(3, "Polygon mismatch, creating new cache. %c, %c", a, b);
+        freeCache(currentCache);
+        return createNewCache(poly, serializedPoly);
+    }
+
+    LWDEBUGF(3, "Polygon match, retaining current cache, %p.", currentCache);
+
+    return currentCache;
 }
 
index db17de7cc462cb0bc753a66c8c164d562761c043..aa82c3ee619ef121da903491134216c5e5c7197d 100644 (file)
@@ -55,5 +55,8 @@ typedef struct
  * it is applicable to the current polygon.\r
  */\r
 RTREE_POLY_CACHE *retrieveCache(LWPOLY *poly, uchar *serializedPoly, RTREE_POLY_CACHE *currentCache);\r
+RTREE_POLY_CACHE *createNewCache(LWPOLY *poly, uchar *serializedPoly);\r
+/* Frees the cache. */\r
+void freeCache(RTREE_POLY_CACHE *cache);\r
 \r
 #endif /* !defined _LIBLWGEOM_H */\r