[gdal-dev] Use of CPLAssert macro

Even Rouault even.rouault at mines-paris.org
Thu Jul 16 14:28:50 EDT 2009


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.

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.

Best regards,

Even

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