From 35b0ff01ae269a4af1e21c9318886b9d7d72928b Mon Sep 17 00:00:00 2001 From: Bborie Park Date: Mon, 9 Jan 2012 20:08:15 +0000 Subject: [PATCH] Make the data of externally-loaded band internally owned so a call to rt_band_destroy() properly frees the memory. git-svn-id: http://svn.osgeo.org/postgis/trunk@8731 b70326c6-7e19-0410-871a-916f4a2858ee --- raster/rt_core/rt_api.c | 14 +++++++++++++- raster/rt_core/rt_api.h | 6 ++++-- raster/test/core/testapi.c | 24 +++++++++++++----------- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/raster/rt_core/rt_api.c b/raster/rt_core/rt_api.c index 76ff011ae..c7961d67f 100644 --- a/raster/rt_core/rt_api.c +++ b/raster/rt_core/rt_api.c @@ -1131,6 +1131,10 @@ 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); + /* band->data content is externally owned */ /* XXX jorgearevalo: not really... rt_band_from_wkb allocates memory for * data.mem @@ -1192,7 +1196,9 @@ rt_band_get_data(rt_band band) { } /** - * Load offline band's data + * Load offline band's data. Loaded data is internally owned + * and should not be released by the caller. Data will be + * released when band is destroyed with rt_band_destroy(). * * @param band : the band who's data to get * @@ -1304,6 +1310,12 @@ rt_band_load_offline_band(rt_band band) { return 1; } + /* band->data.offline.mem not NULL, free first */ + if (band->data.offline.mem != NULL) { + rtdealloc(band->data.offline.mem); + band->data.offline.mem = NULL; + } + band->data.offline.mem = _band->data.mem; rt_band_destroy(_band); diff --git a/raster/rt_core/rt_api.h b/raster/rt_core/rt_api.h index 4e6ed8d09..c19884484 100644 --- a/raster/rt_core/rt_api.h +++ b/raster/rt_core/rt_api.h @@ -404,7 +404,9 @@ uint16_t rt_band_get_height(rt_band band); void* rt_band_get_data(rt_band band); /** - * Load offline band's data + * Load offline band's data. Loaded data is internally owned + * and should not be released by the caller. Data will be + * released when band is destroyed with rt_band_destroy(). * * @param band : the band who's data to get * @@ -1426,7 +1428,7 @@ struct rt_raster_t { struct rt_extband_t { uint8_t bandNum; /* 0-based */ char* path; /* externally owned ? */ - void *mem; /* loaded external band data */ + void *mem; /* loaded external band data, internally owned */ }; struct rt_band_t { diff --git a/raster/test/core/testapi.c b/raster/test/core/testapi.c index 12eabd04e..0a2c45d66 100644 --- a/raster/test/core/testapi.c +++ b/raster/test/core/testapi.c @@ -22,7 +22,7 @@ addBand(rt_raster raster, rt_pixtype pixtype, int hasnodata, double nodataval) uint16_t height = rt_raster_get_height(raster); datasize = rt_pixtype_size(pixtype)*width*height; - mem = malloc(datasize); + mem = rtalloc(datasize); rt_band band = rt_band_new_inline(width, height, pixtype, hasnodata, nodataval, mem); @@ -39,8 +39,10 @@ deepRelease(rt_raster raster) for (i=0; i= 0; i--) free(exprset[i]); - free(exprset); + for (i = cnt - 1; i >= 0; i--) rtdealloc(exprset[i]); + rtdealloc(exprset); deepRelease(raster); mem = rt_band_get_data(newband); - if (mem) free(mem); + if (mem) rtdealloc(mem); rt_band_destroy(newband); } @@ -1575,7 +1577,7 @@ static void testGDALRasterize() { /* hex to byte */ wkb_len = (int) ceil(((double) strlen(wkb_hex)) / 2); - wkb = (unsigned char *) malloc(sizeof(unsigned char) * wkb_len); + wkb = (unsigned char *) rtalloc(sizeof(unsigned char) * wkb_len); for (i = 0; i < wkb_len; i++) { sscanf(pos, "%2hhx", &wkb[i]); pos += 2; @@ -1595,7 +1597,7 @@ static void testGDALRasterize() { NULL ); - free(wkb); + rtdealloc(wkb); CHECK(raster); CHECK((rt_raster_get_width(raster) == 100)); -- 2.40.0