[Mapserver-dev] [PATCH] mapraster.c - fixing memory leaks

Mladen Turk mturk at apache.org
Wed Jun 2 12:18:59 EDT 2004


This is a multi-part message in MIME format.

------=_NextPart_000_005F_01C448CE.0325AC80
Content-Type: multipart/mixed;
	boundary="----=_NextPart_001_0060_01C448CE.0325AC80"


------=_NextPart_001_0060_01C448CE.0325AC80
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: 7bit


Hi all,

Mapraster has a memory leak bug not freeing allocated temporary layer. 
The quickest solution would be to just add the tlptemp variable, but I've
added standard 'cleanup:' label for function exiting. 

Having something like mpatrol gives:

                Total unfreed bytes:        92899
                Peak memory usage:        1699139
                Largest allocation:        235750
                Smallest allocation:            1
                Total Allocations:            513
                Total Frees:                  505
                Peak Allocations:             385


The patch for mapraster.c gives:

                Total unfreed bytes:           14
                Peak memory usage:        1699139
                Largest allocation:        235750
                Smallest allocation:            1
                Total Allocations:            513
                Total Frees:                  512
                Peak Allocations:             385

So we've save cca 92K of memory on each request.

Here is the patch:
(I can file a bug report if needed)

There is still 14 bytes left hanging but the reason is in the getString()
from mapfile.c used somewhere :). I'll try to hunt that down.

MT.

------=_NextPart_001_0060_01C448CE.0325AC80
Content-Type: text/plain;
	name="mapraster.patch.txt"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="mapraster.patch.txt"

Index: mapraster.c=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: /data2/cvsroot/mapserver/mapraster.c,v=0A=
retrieving revision 1.117=0A=
diff -u -r1.117 mapraster.c=0A=
--- mapraster.c	1 Jun 2004 14:33:43 -0000	1.117=0A=
+++ mapraster.c	2 Jun 2004 16:05:19 -0000=0A=
@@ -1201,6 +1201,7 @@=0A=
   char *filename=3DNULL, tilename[MS_MAXPATHLEN];=0A=
 =0A=
   layerObj *tlp=3DNULL; // pointer to the tile layer either real or =
temporary=0A=
+  int tlptemp =3D 0;=0A=
   int tileitemindex=3D-1, tilelayerindex=3D-1;=0A=
   shapeObj tshp;=0A=
 =0A=
@@ -1283,6 +1284,7 @@=0A=
         msSetError(MS_MEMERR, "Error allocating temporary layerObj.", =
"msDrawRasterLayerLow()");=0A=
         return(MS_FAILURE);=0A=
       }=0A=
+      tlptemp =3D 1;=0A=
       initLayer(tlp, map);=0A=
 =0A=
       // set a few parameters for a very basic shapefile-based layer=0A=
@@ -1293,12 +1295,12 @@=0A=
       tlp =3D &(layer->map->layers[tilelayerindex]);=0A=
       =0A=
     status =3D msLayerOpen(tlp);=0A=
-    if(status !=3D MS_SUCCESS) return(MS_FAILURE);=0A=
+    if(status !=3D MS_SUCCESS) goto cleanup;=0A=
 =0A=
     // build item list (no annotation) since we may have to classify =
the shape, plus we want the tileitem=0A=
     status =3D msLayerWhichItems(tlp, MS_TRUE, MS_FALSE, =
layer->tileitem);=0A=
-    if(status !=3D MS_SUCCESS) return(MS_FAILURE);=0A=
- =0A=
+    if(status !=3D MS_SUCCESS) goto cleanup;=0A=
+=0A=
     // get the tileitem index=0A=
     for(i=3D0; i<tlp->numitems; i++) {=0A=
       if(strcasecmp(tlp->items[i], layer->tileitem) =3D=3D 0) {=0A=
@@ -1308,7 +1310,7 @@=0A=
     }=0A=
     if(i =3D=3D tlp->numitems) { // didn't find it=0A=
       msSetError(MS_MEMERR, "Could not find attribute %s in =
tileindex.", "msDrawRasterLayerLow()", layer->tileitem);=0A=
-      return(MS_FAILURE);=0A=
+      if(status !=3D MS_SUCCESS) goto cleanup;=0A=
     }=0A=
  =0A=
     searchrect =3D map->extent;=0A=
@@ -1317,14 +1319,14 @@=0A=
     if((map->projection.numargs > 0) && (layer->projection.numargs > =
0)) msProjectRect(&map->projection, &layer->projection, &searchrect);=0A=
 #endif=0A=
     status =3D msLayerWhichShapes(tlp, searchrect);=0A=
-    if(status !=3D MS_SUCCESS) return(MS_FAILURE); // TODO: probably =
need more clean up here=0A=
+    if(status !=3D MS_SUCCESS) goto cleanup;=0A=
   }=0A=
 =0A=
   done =3D MS_FALSE;=0A=
   while(done !=3D MS_TRUE) { =0A=
     if(layer->tileindex) {=0A=
       status =3D msLayerNextShape(tlp, &tshp);=0A=
-      if(status =3D=3D MS_FAILURE) return(MS_FAILURE);=0A=
+    if(status !=3D MS_SUCCESS) goto cleanup;=0A=
       if(status =3D=3D MS_DONE) break; // no more tiles/images=0A=
        =0A=
       if(layer->data =3D=3D NULL) // assume whole filename is in =
attribute field=0A=
@@ -1355,7 +1357,7 @@=0A=
     if((memcmp(dd,"II*\0",4)=3D=3D0 || memcmp(dd,"MM\0*",4)=3D=3D0) && =
!force_gdal) {=0A=
       status =3D drawTIFF(map, layer, img, filename);=0A=
       if(status =3D=3D -1) {=0A=
-        return(MS_FAILURE);=0A=
+        goto cleanup;=0A=
       }=0A=
       continue;=0A=
     }=0A=
@@ -1363,7 +1365,7 @@=0A=
     if(memcmp(dd,"GIF8",4)=3D=3D0 && !force_gdal ) {=0A=
       status =3D drawGIF(map, layer, img, filename);=0A=
       if(status =3D=3D -1) {=0A=
-        return(MS_FAILURE);=0A=
+        goto cleanup;=0A=
       }=0A=
       continue;=0A=
     }=0A=
@@ -1371,7 +1373,7 @@=0A=
     if(memcmp(dd,PNGsig,8)=3D=3D0 && !force_gdal) {=0A=
       status =3D drawPNG(map, layer, img, filename);=0A=
       if(status =3D=3D -1) {=0A=
-        return(MS_FAILURE);=0A=
+        goto cleanup;=0A=
       }=0A=
       continue;=0A=
     }=0A=
@@ -1379,7 +1381,7 @@=0A=
     if(memcmp(dd,JPEGsig,3)=3D=3D0 && !force_gdal) {=0A=
       status =3D drawJPEG(map, layer, img, filename);=0A=
       if(status =3D=3D -1) {=0A=
-        return(MS_FAILURE);=0A=
+        goto cleanup;=0A=
       }=0A=
       continue;=0A=
     }=0A=
@@ -1422,7 +1424,8 @@=0A=
                                    szLongMsg);=0A=
 =0A=
                         msReleaseLock( TLOCK_GDAL );=0A=
-                        return(MS_FAILURE);=0A=
+                        status =3D MS_FAILURE;=0A=
+                        goto cleanup;=0A=
                     }=0A=
                 }=0A=
             }=0A=
@@ -1461,7 +1464,8 @@=0A=
             {=0A=
                 GDALClose( hDS );=0A=
                 msReleaseLock( TLOCK_GDAL );=0A=
-                return MS_FAILURE;=0A=
+                status =3D MS_FAILURE;=0A=
+                goto cleanup;=0A=
             }=0A=
 =0A=
             GDALClose( hDS );=0A=
@@ -1485,7 +1489,8 @@=0A=
       if( layer->debug || map->debug )=0A=
           msDebug( "Unable to open file %s for layer %s ... fatal =
error.\n", filename, layer->name );=0A=
 =0A=
-      return(MS_FAILURE);=0A=
+      status =3D MS_FAILURE;=0A=
+      goto cleanup;=0A=
 #else=0A=
       if( layer->debug || map->debug )=0A=
           msDebug( "Unable to open file %s for layer %s ... ignoring =
this missing data.\n", filename, layer->name );=0A=
@@ -1501,20 +1506,21 @@=0A=
     if(status !=3D 0) {=0A=
       if (status =3D=3D -2) msSetError(MS_IMGERR, "Error reading EPPL =
file; probably corrupt.", "msDrawEPP()");=0A=
       if (status =3D=3D -1) msSetError(MS_IMGERR, "Unrecognized or =
unsupported image format", "msDrawRaster()");=0A=
-      return(MS_FAILURE);=0A=
+      goto cleanup;=0A=
     }=0A=
     continue;=0A=
   } // next tile=0A=
 =0A=
+cleanup:=0A=
   if(layer->tileindex) { // tiling clean-up=0A=
     msLayerClose(tlp);=0A=
-    if(tileitemindex =3D=3D -1) {=0A=
+    if(tlptemp) {=0A=
       freeLayer(tlp);=0A=
       free(tlp);=0A=
     }=0A=
   }=0A=
-=0A=
-  return 0;=0A=
+  =0A=
+  return status;=0A=
 }=0A=
 =0A=
 =
/************************************************************************=
/=0A=
=0A=

------=_NextPart_001_0060_01C448CE.0325AC80--

------=_NextPart_000_005F_01C448CE.0325AC80
Content-Type: application/x-pkcs7-signature;
	name="smime.p7s"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
	filename="smime.p7s"

MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAQAAoIIJADCCAogw
ggHxoAMCAQICAwxGPTANBgkqhkiG9w0BAQQFADBiMQswCQYDVQQGEwJaQTElMCMGA1UEChMcVGhh
d3RlIENvbnN1bHRpbmcgKFB0eSkgTHRkLjEsMCoGA1UEAxMjVGhhd3RlIFBlcnNvbmFsIEZyZWVt
YWlsIElzc3VpbmcgQ0EwHhcNMDQwNTA4MTc0OTIyWhcNMDUwNTA4MTc0OTIyWjBpMR8wHQYDVQQD
ExZUaGF3dGUgRnJlZW1haWwgTWVtYmVyMSUwIwYJKoZIhvcNAQkBFhZtbGFkZW4udHVya0BnaXNk
YXRhLmhyMR8wHQYJKoZIhvcNAQkBFhBtdHVya0BhcGFjaGUub3JnMIGfMA0GCSqGSIb3DQEBAQUA
A4GNADCBiQKBgQDDFpTL3QpMpzZNX4hMRwNoTAcJtSF18PnJf3oUryvGmggiy9cbJAuScvfVku2R
DDeBdJaVqDb83nYQydW6JvZI7m2oKApNRV9veadN/T7Fq81QcSBAQgXRyNWCMyl832zOxDrZNchH
kgQzkm67IMHXxDd8LYAxAj8eFFLuV+g29QIDAQABo0UwQzAzBgNVHREELDAqgRZtbGFkZW4udHVy
a0BnaXNkYXRhLmhygRBtdHVya0BhcGFjaGUub3JnMAwGA1UdEwEB/wQCMAAwDQYJKoZIhvcNAQEE
BQADgYEAtF/h4HA4Zt+3J1a3P0y4Yllqa3srAR+oHJMpQjFLSoUYHkwBQW7iuCWCuzthoIbW/Wuk
g9ww/RSSCJUOFuHham7kClwbWFoRZL9i61L3vfoM3AMKVOj2T0JmoAV+4Ib6lGSaX7DywGbZB5qd
9gpfITF2Cl56XdZ8EXHA2kj2I9cwggMtMIIClqADAgECA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WrGH20Trtuigd
2AAAAAAAAA==

------=_NextPart_000_005F_01C448CE.0325AC80--




More information about the mapserver-dev mailing list