Skip to content

Commit

Permalink
Fix Memory leak in UVRaster layers (#4706)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Boudreault committed Aug 6, 2013
1 parent 5589896 commit 728d64d
Showing 1 changed file with 54 additions and 39 deletions.
93 changes: 54 additions & 39 deletions mapuvraster.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,23 @@ int msUVRASTERLayerWhichShapes(layerObj *layer, rectObj rect, int isQuery)
{
uvRasterLayerInfo *uvlinfo = (uvRasterLayerInfo *) layer->layerinfo;
imageObj *image_tmp;
mapObj map_tmp;
outputFormatObj *outputformat = NULL;
mapObj *map_tmp;
double map_cellsize;
unsigned int spacing;
int width, height, u_src_off, v_src_off, i, x, y;
char **alteredProcessing = NULL;
char **savedProcessing = NULL;

/*
** Allocate mapObj structure
*/
map_tmp = (mapObj *)msSmallCalloc(sizeof(mapObj),1);
if(initMap(map_tmp) == -1) { /* initialize this map */
msFree(map_tmp);
return(MS_FAILURE);
}

if (layer->debug)
msDebug("Entering msUVRASTERLayerWhichShapes().\n");

Expand All @@ -358,6 +368,7 @@ int msUVRASTERLayerWhichShapes(layerObj *layer, rectObj rect, int isQuery)
"msUVRASTERLayerWhichShapes()" );
return MS_FAILURE;
}

/* -------------------------------------------------------------------- */
/* Determine desired spacing. Default to 32 if not otherwise set */
/* -------------------------------------------------------------------- */
Expand All @@ -371,58 +382,59 @@ int msUVRASTERLayerWhichShapes(layerObj *layer, rectObj rect, int isQuery)
height = (int)ceil(layer->map->height/spacing);
map_cellsize = MS_MAX(MS_CELLSIZE(rect.minx, rect.maxx,layer->map->width),
MS_CELLSIZE(rect.miny,rect.maxy,layer->map->height));
map_tmp.cellsize = map_cellsize*spacing;
map_tmp->cellsize = map_cellsize*spacing;

if (layer->debug)
msDebug("msUVRASTERLayerWhichShapes(): width: %d, height: %d, cellsize: %g\n",
width, height, map_tmp.cellsize);
width, height, map_tmp->cellsize);

/* Initialize our dummy map */
MS_INIT_COLOR(map_tmp.imagecolor, 255,255,255,255);
map_tmp.resolution = layer->map->resolution;
map_tmp.defresolution = layer->map->defresolution;
map_tmp.outputformat = (outputFormatObj *) msSmallCalloc(1,sizeof(outputFormatObj));
uvlinfo->band_count = map_tmp.outputformat->bands = 2;
map_tmp.outputformat->name = NULL;
map_tmp.outputformat->driver = NULL;
map_tmp.outputformat->refcount = 0;
map_tmp.outputformat->vtable = NULL;
map_tmp.outputformat->device = NULL;
map_tmp.outputformat->renderer = MS_RENDER_WITH_RAWDATA;
map_tmp.outputformat->imagemode = MS_IMAGEMODE_FLOAT32;

map_tmp.configoptions = layer->map->configoptions;
map_tmp.mappath = layer->map->mappath;
map_tmp.shapepath = layer->map->shapepath;
map_tmp.extent.minx = rect.minx-(0.5*map_cellsize)+(0.5*map_tmp.cellsize);
map_tmp.extent.miny = rect.miny-(0.5*map_cellsize)+(0.5*map_tmp.cellsize);
map_tmp.extent.maxx = map_tmp.extent.minx+((width-1)*map_tmp.cellsize);
map_tmp.extent.maxy = map_tmp.extent.miny+((height-1)*map_tmp.cellsize);
map_tmp.gt.rotation_angle = 0.0;

msInitProjection(&map_tmp.projection);
msCopyProjection(&map_tmp.projection, &layer->projection);
MS_INIT_COLOR(map_tmp->imagecolor, 255,255,255,255);
map_tmp->resolution = layer->map->resolution;
map_tmp->defresolution = layer->map->defresolution;

outputformat = (outputFormatObj *) msSmallCalloc(1,sizeof(outputFormatObj));
outputformat->bands = uvlinfo->band_count = 2;
outputformat->name = NULL;
outputformat->driver = NULL;
outputformat->refcount = 0;
outputformat->vtable = NULL;
outputformat->device = NULL;
outputformat->renderer = MS_RENDER_WITH_RAWDATA;
outputformat->imagemode = MS_IMAGEMODE_FLOAT32;
msAppendOutputFormat(map_tmp, outputformat);

msCopyHashTable(map_tmp->configoptions, layer->map->configoptions);
map_tmp->mappath = msStrdup(layer->map->mappath);
map_tmp->shapepath = msStrdup(layer->map->shapepath);
map_tmp->extent.minx = rect.minx-(0.5*map_cellsize)+(0.5*map_tmp->cellsize);
map_tmp->extent.miny = rect.miny-(0.5*map_cellsize)+(0.5*map_tmp->cellsize);
map_tmp->extent.maxx = map_tmp->extent.minx+((width-1)*map_tmp->cellsize);
map_tmp->extent.maxy = map_tmp->extent.miny+((height-1)*map_tmp->cellsize);
map_tmp->gt.rotation_angle = 0.0;

msCopyProjection(&map_tmp->projection, &layer->projection);

if (layer->debug == 5)
msDebug("msUVRASTERLayerWhichShapes(): extent: %g %d %g %g\n",
map_tmp.extent.minx, map_tmp.extent.miny,
map_tmp.extent.maxx, map_tmp.extent.maxy);
map_tmp->extent.minx, map_tmp->extent.miny,
map_tmp->extent.maxx, map_tmp->extent.maxy);

/* important to use that function, to compute map
geotransform, used by the resampling*/
msMapSetSize(&map_tmp, width, height);
msMapSetSize(map_tmp, width, height);

if (layer->debug == 5)
msDebug("msUVRASTERLayerWhichShapes(): geotransform: %g %g %g %g %g %g\n",
map_tmp.gt.geotransform[0], map_tmp.gt.geotransform[1],
map_tmp.gt.geotransform[2], map_tmp.gt.geotransform[3],
map_tmp.gt.geotransform[4], map_tmp.gt.geotransform[5]);
map_tmp->gt.geotransform[0], map_tmp->gt.geotransform[1],
map_tmp->gt.geotransform[2], map_tmp->gt.geotransform[3],
map_tmp->gt.geotransform[4], map_tmp->gt.geotransform[5]);

uvlinfo->extent = map_tmp.extent;
uvlinfo->extent = map_tmp->extent;

image_tmp = msImageCreate(width, height, map_tmp.outputformat,
NULL, NULL, map_tmp.resolution, map_tmp.defresolution,
&(map_tmp.imagecolor));
image_tmp = msImageCreate(width, height, map_tmp->outputformatlist[0],
NULL, NULL, map_tmp->resolution, map_tmp->defresolution,
&(map_tmp->imagecolor));

/* Default set to AVERAGE resampling */
if( CSLFetchNameValue( layer->processing, "RESAMPLE" ) == NULL ) {
Expand All @@ -434,14 +446,16 @@ int msUVRASTERLayerWhichShapes(layerObj *layer, rectObj rect, int isQuery)
layer->processing = alteredProcessing;
}

if (msDrawRasterLayerLow(&map_tmp, layer, image_tmp, NULL ) == MS_FAILURE) {
if (msDrawRasterLayerLow(map_tmp, layer, image_tmp, NULL ) == MS_FAILURE) {
msSetError(MS_MISCERR, "Unable to draw raster data.", NULL, "msUVRASTERLayerWhichShapes()" );
return MS_FAILURE;
}

/* restore the saved processing */
if (alteredProcessing != NULL)
if (alteredProcessing != NULL) {
layer->processing = savedProcessing;
CSLDestroy(alteredProcessing);
}

/* free old query arrays */
if (uvlinfo->u) {
Expand Down Expand Up @@ -484,6 +498,7 @@ int msUVRASTERLayerWhichShapes(layerObj *layer, rectObj rect, int isQuery)
}

msFreeImage(image_tmp); /* we do not need the imageObj anymore */
msFreeMap(map_tmp);

uvlinfo->next_shape = 0;

Expand Down

0 comments on commit 728d64d

Please sign in to comment.