[gdal-dev] Catch a GDALOpen exception

Mateusz Loskot mateusz at loskot.net
Wed Apr 6 16:15:12 EDT 2011


Even Rouault wrote:
> Mateusz Loskot wrote:
>> This is interesting. Could you give example?
>> How is checking if GDALOpen returned NULL and prematurely exiting
>> different to throwing exception?
>
> 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.
> [...]
> 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;
>     }

Even,

Yes, you are right. I missed you mean the CPLError. In case of custom
throwing handler it will jump out of the context. I do throw exceptions
reacting on GDAL errors, but not from within GDAL internal context,
like the custom handler.

>> 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().

Yes, that's what I was missing.

But, now this "before" should read "after", I think:
"no guarantee that the GDAL error is emitted after the cleanup of
ressources has been done", so leaks possible.

Best regards,
-- 
Mateusz Loskot
http://mateusz.loskot.net



More information about the gdal-dev mailing list