[Gdal-dev] Where does the CPLError list get freed?

Craig Miller craig.miller at spatialminds.com
Wed Oct 11 20:44:12 EDT 2006

I'm on the warpath to get rid of a few niggling memory leaks that I'm
detecting.  A couple of items have me a bit puzzled at this point and I was
hoping to get a bit of clarification.

1.  If I call CPLGetLastErrorMsg() when there is no error, it obtains a
CPLErrorContext via CPLGetErrorContext() which creates an empty
CPLErrorContext and adds it to the list if it doesn't exist.  I've looked a
fair amount now, but can't seem to locate where this memory gets freed.

2.  Does CPLErrorReset clobber any szLastErrMsg that may have already been

 * Erase any traces of previous errors.
 * This is normally used to ensure that an error which has been recovered
 * from does not appear to be still in play with high level functions.

void CPL_STDCALL CPLErrorReset()
    CPLErrorContext *psCtx = CPLGetErrorContext();

    psCtx->nLastErrNo = CPLE_None;
    psCtx->szLastErrMsg[0] = '\0';
    psCtx->eLastErrType = CE_None;

Here is the stack trace showing where the memory is being allocated that
isn't being freed.

Call Stack:
    c:\vault\gdal\port\cpl_vsisimple.cpp (323): VSICalloc
    c:\vault\gdal\port\cpl_conv.cpp (240): CPLCalloc
    c:\vault\gdal\port\cpl_error.cpp (122): CPLGetErrorContext
    c:\vault\gdal\port\cpl_error.cpp (436): CPLGetLastErrorMsg
    c:\vault\freeworlddesktop\geolib\geodatabase.cpp (53):

Line 53 of CGeoDatabase::SetConnection is:
   const char* errMsg = CPLGetLastErrorMsg();

CPLErrorReset() is called 2 lines later.

The documentation for CPLGetLastErrorMsg indicates that the string returned
should not be freed by the caller.

Thanks for any pointers (pun intended).  :-)  I appreciate the help.


More information about the Gdal-dev mailing list