[gdal-dev] Use of CPLAssert macro
Jorge Arévalo
jorge.arevalo at gmail.com
Fri Jul 17 05:09:48 EDT 2009
Hello,
2009/7/16 Even Rouault <even.rouault at mines-paris.org>:
> Jorge,
>
> CPLAssert should be rarely used. You should generally use CPLError instead.
> I've regularly removed remaining CPLAssert from GDAL code base to replace
> them with CPLError when it was appropriate.
>
> As you noted, CPLAssert causes an abort to the process, which is generally a
> bad thing from users point of view. Furthermore, CPLAssert expands to nothing
> for non-DEBUG builds of GDAL, which means that you can't rely on it to detect
> error situations for production builds of GDAL. It's really a debug-only
> thing for developers to detect early logic errors, "impossible" conditions.
>
> In all other situations (bad user input, unexpected/corrupted input data,
> etc..), an appropriate error message should be issued with CPLError() and you
> should properly return (!= abort) from the function. For example, if it
> occurs in the ::Open() method of a dataset, you should delete any still
> allocated memory and return NULL.
Yes, that's what I've done.
>
> In your example, if you only support regular_blocking arrangement, you should
> use CPLError() and not CPLAssert(). It's not a logic error, but rather
> unexpected/unsupported form of data. The CPLE_NotSupported error code would
> be the most appropriate.
Ok, I'm using CPLE_AppDefined, but CPLE_NotSupported is a much better
idea in that case. Many thanks.
Best regards
Jorge
> Le Thursday 16 July 2009 12:12:09 Jorge Arévalo, vous avez écrit :
>> Hello,
>>
>> Please, correct me if I'm wrong:
>>
>> The CPLAssert macro should be used only to report errors that force
>> application to abort, like when we can't read a raster band, for
>> example. For errors that only has sense in the context of the code
>> that we're developing, we may perform a "common" error checking and,
>> if fails, call CPLError with the severity needed (CE_Failure...).
>>
>> I mean, if in my code (GDAL WKT Raster driver), I only want to read
>> raster with regular_blocking arrangement, for example, this isn't an
>> "error", but I want to force a fail, and abort. Then, I check the
>> arrangement, and call CPLError if fails.
>>
>> Am I right? Is this the point to difference between calling CPLAssert
>> and check the error by myself and call CPLError?
>>
>> Thanks in advance
>>
>> Best regards,
>> Jorge
>> _______________________________________________
>> gdal-dev mailing list
>> gdal-dev at lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/gdal-dev
>
>
>
More information about the gdal-dev
mailing list