[gdal-dev] Catch a GDALOpen exception
Even Rouault
even.rouault at mines-paris.org
Wed Apr 6 15:52:41 EDT 2011
> This is interesting. Could you give example?
> How is checking if GDALOpen returned NULL and prematurely exiting
> different to throwing exception?
Mateusz,
The ::Open() method of GDAL drivers is supposed to make all necessary
cleanups, even in case of errors, before returning NULL.
But if you install a custom CPL error handler that throws a C++ exception, and
a CPLError() is emitted in GDAL driver code, it will directly unwind to the
matching catch( xx ) { } in your code. This might not be a big deal in most
situations, but in theory, there could be cases where some cleanups haven't
been yet done.
... After a few seconds of digging, here's an example (and I'm sure there are
many other similar cases). Let's look at the Open() method of the BMP driver
You have :
if (poDS->nRasterXSize <= 0 || poDS->nRasterYSize <= 0)
{
CPLError( CE_Failure, CPLE_AppDefined,
"Invalid dimensions : %d x %d",
poDS->nRasterXSize, poDS->nRasterYSize);
delete poDS;
return NULL;
}
So if in your custom error handler you throw a C++ exception, the dataset will
not be destroyed, which means both a memory leak and a file descriptor leak in
that instance.
>
> It should be possible to react on GDAL errors and throw exception
> as long as certain cleanup is performed.
>
> > (there's no guarantee that the GDAL error is emitted
> > before the cleanup of ressources has been done).
>
> What error emission you mean? Returning NULL? Call to abort()?
An error emitted by CPLError().
>
> I have written code intensively throwing exceptions around GDAL,
> even CPLErr is used one of exception type. Code is well-tested
> and I haven't noticed problems.
>
> Hobu's solution might be incomplete, but gives rough idea which is valid
> if used properly. In any case, details are needed to be able to judge.
>
> Best regards,
More information about the gdal-dev
mailing list