]> granicus.if.org Git - postgis/commitdiff
Changed memory handling of offline band path. Now explicitly owned
authorBborie Park <bkpark at ucdavis.edu>
Wed, 19 Dec 2012 23:26:33 +0000 (23:26 +0000)
committerBborie Park <bkpark at ucdavis.edu>
Wed, 19 Dec 2012 23:26:33 +0000 (23:26 +0000)
internally

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

raster/rt_core/rt_api.c
raster/rt_core/rt_api.h

index 06a3a2a3ff31e29cf889e703d97d2a03886103a5..11b111e46f46d0273b895a0ca2fe7106ce08b2fd 100644 (file)
@@ -1375,6 +1375,7 @@ rt_band_new_offline(
        uint8_t bandNum, const char* path
 ) {
        rt_band band = NULL;
+       int pathlen = 0;
 
        assert(NULL != path);
 
@@ -1409,8 +1410,16 @@ rt_band_new_offline(
 
        band->data.offline.bandNum = bandNum;
 
-       /* memory for data.offline.path should be managed externally */
-       band->data.offline.path = (char *) path;
+       /* memory for data.offline.path is managed internally */
+       pathlen = strlen(path);
+       band->data.offline.path = rtalloc(sizeof(char) * (pathlen + 1));
+       if (band == NULL) {
+               rterror("rt_band_new_offline: Out of memory allocating offline path");
+               rtdealloc(band);
+               return NULL;
+       }
+       memcpy(band->data.offline.path, path, pathlen);
+       band->data.offline.path[pathlen] = '\0';
 
        band->data.offline.mem = NULL;
 
@@ -1494,11 +1503,17 @@ rt_band_destroy(rt_band band) {
 
     RASTER_DEBUGF(3, "Destroying rt_band @ %p", band);
 
-               /* offline band and has data, free as data is internally owned */
-               if (band->offline && band->data.offline.mem != NULL)
-                       rtdealloc(band->data.offline.mem);
+               /* offline band */
+               if (band->offline) {
+                       /* memory cache */
+                       if (band->data.offline.mem != NULL)
+                               rtdealloc(band->data.offline.mem);
+                       /* offline file path */
+                       if (band->data.offline.path != NULL)
+                               rtdealloc(band->data.offline.path);
+               }
                /* inline band and band owns the data */
-               else if (!band->offline && band->data.mem != NULL && band->ownsdata)
+               else if (band->data.mem != NULL && band->ownsdata)
                        rtdealloc(band->data.mem);
 
     rtdealloc(band);
@@ -7313,8 +7328,8 @@ rt_band_from_wkb(uint16_t width, uint16_t height,
             }
 
             band->ownsdata = 0;
-            band->data.offline.path = rtalloc(sz + 1);
 
+            band->data.offline.path = rtalloc(sz + 1);
             memcpy(band->data.offline.path, *ptr, sz);
             band->data.offline.path[sz] = '\0';
 
@@ -8252,13 +8267,24 @@ rt_raster_deserialize(void* serialized, int header_only) {
         assert(!((ptr - beg) % pixbytes));
 
         if (band->offline) {
+                                       int pathlen = 0;
             /* Read band number */
             band->data.offline.bandNum = *ptr;
             ptr += 1;
 
             /* Register path */
-            band->data.offline.path = (char*) ptr;
-            ptr += strlen(band->data.offline.path) + 1;
+                                               pathlen = strlen((char*) ptr);
+                                               band->data.offline.path = rtalloc(sizeof(char) * (pathlen + 1));
+                                               if (band->data.offline.path == NULL) {
+                                                       rterror("rt_raster_deserialize: Unable to allocate momory for offline band path");
+                                                       rt_band_destroy(band);
+                                                       rt_raster_destroy(rast);
+                                                       return NULL;
+                                               }
+
+            memcpy(band->data.offline.path, ptr, pathlen);
+                                               band->data.offline.path[pathlen] = '\0';
+            ptr += pathlen + 1;
 
                                                band->data.offline.mem = NULL;
         } else {
@@ -11084,6 +11110,7 @@ rt_raster_gdal_rasterize(const unsigned char *wkb,
                OSRExportToWkt(src_sr, &_srs);
 
                cplerr = GDALSetProjection(_ds, _srs);
+               CPLFree(_srs);
                if (cplerr != CE_None) {
                        rterror("rt_raster_gdal_rasterize: Could not set projection on GDALDataset");
 
index a5578a3a83a5df6ecf01e4553af6eb959efe82e1..d1d7674817880e20d961a30dcd4968a0dea10e3d 100644 (file)
@@ -2167,7 +2167,7 @@ struct rt_raster_t {
 
 struct rt_extband_t {
     uint8_t bandNum; /* 0-based */
-    char* path; /* externally owned ? */
+    char* path; /* internally owned */
                void *mem; /* loaded external band data, internally owned */
 };