[Gdal-dev] Re: Memory leak under Matlab (a follow up from - importFromWkt() problem)

Joaquim Luis jluis at ualg.pt
Mon Sep 4 12:37:19 EDT 2006


Mateusz,
> Sounds strange. Is there any chance you're misusing MEX memory
> management functions or may be you're following one of MATLAB
> buggy examples, like:
> http://www.mathworks.com/support/solutions/data/1-1NOXOX.html
>
> May be you're using mexMakeMemoryPersistent in loop or similar call,
> so memory is not cleared after your MEX is unloaded.
> Note, it's only my surmise.
>   

No, that should not be the problem.

> Are you using MATLAB on Windows?
>   
Yes.
> Have you tried to run your MEX with a memory leak checker attached?
> If you're using MATLAB on Windows, I'd try to build MEX dll with
> VLD leak detector [1] but I'm not sure if it will give you usable
> and meaningful report when working with MATLAB.
>
> [1] http://www.codeproject.com/tools/visualleakdetector.asp
>   

OK, I'll see if that is possible.

> Another option is to do some simple  tests of memory
> allocation/deallocation and observe how MATLAB will behave.
>   

I tried that and never found a case with a memory leak. All cases that I 
found
occur when 3rth party libraries are used by the ML mexs (eg. gdal, gmt & 
opencv).
That is why I anticipate that the Mathworks support will be difficult to 
convince,
and that is also why that I'm trying to build the strongest as possible 
case (or find out
what is going on) before contacting them.

For example the following very short peace of code already produces the 
memory
leak, but in order to see (in the Windows Task Manager) it I have to run 
the code many times.
Something like:

for(i=1:5000);t=test_leak('+proj=longlat');end

However, in the original code from where this excerpt comes, the leak 
was sufficiently  large
to be seen only after a couple of executions.

Is there anyone who could test this on a system other than Windows?

Thanks

Joaquim Luis


#include "mex.h"
#include "gdal.h"
#include "ogr_spatialref.h"

/* 
--------------------------------------------------------------------------- 
*/
/* Matlab Gateway routine */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray 
*prhs[]) {
    char    *pszSrcSRS = NULL, *pszSrcWKT = NULL;
    char    *pszDstSRS = NULL, *pszDstWKT = NULL;
    OGRSpatialReference oSrcSRS, oDstSRS;

    if (nrhs == 1 && mxIsChar(prhs[0])) {
        pszSrcSRS = (char *)mxArrayToString(prhs[0]);

        if( oSrcSRS.SetFromUserInput( pszSrcSRS ) != OGRERR_NONE )
            mexErrMsgTxt("OGRPROJ: Translating SRS string failed.");

        oSrcSRS.exportToPrettyWkt( &pszSrcWKT, FALSE );
        if (nlhs == 1)
            plhs[0] = mxCreateString(pszSrcWKT);
        else
            mexPrintf("%s",pszSrcWKT);

        OGRFree(pszSrcWKT);
    }
}






More information about the Gdal-dev mailing list