From: Paul Ramsey Date: Thu, 25 Sep 2008 19:18:37 +0000 (+0000) Subject: More small memory leaks removed. X-Git-Tag: 1.4.0b1~703 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7c46722d86148151c127dd699292c4000b01dab8;p=postgis More small memory leaks removed. git-svn-id: http://svn.osgeo.org/postgis/trunk@3009 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/lwgeom/lwgeom_rtree.c b/lwgeom/lwgeom_rtree.c index 5fe62ae96..8589540a3 100644 --- a/lwgeom/lwgeom_rtree.c +++ b/lwgeom/lwgeom_rtree.c @@ -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; } diff --git a/lwgeom/lwgeom_rtree.h b/lwgeom/lwgeom_rtree.h index db17de7cc..aa82c3ee6 100644 --- a/lwgeom/lwgeom_rtree.h +++ b/lwgeom/lwgeom_rtree.h @@ -55,5 +55,8 @@ typedef struct * it is applicable to the current polygon. */ RTREE_POLY_CACHE *retrieveCache(LWPOLY *poly, uchar *serializedPoly, RTREE_POLY_CACHE *currentCache); +RTREE_POLY_CACHE *createNewCache(LWPOLY *poly, uchar *serializedPoly); +/* Frees the cache. */ +void freeCache(RTREE_POLY_CACHE *cache); #endif /* !defined _LIBLWGEOM_H */