This error handling module extends CPL's error handler by adding error tracing and automatic memory deallocation in case of an error. Like in CPL the current error state is indicated by the cpl_error_code
(returned by the function cpl_error_get_code()
).
The error tracing makes it possible to see where (source file, function name, line number) an error first occured, as well as the sequence of function calls preceding the error. A typical output looks like:
An error occured, dumping error trace:
Wavelength calibration did not converge. After 13 iterations the RMS was
0.300812 pixels. Try to improve
the initial guess solution (The iterative process did not converge)
in [3]sinfo_wavecal_identify() at sinfo_wavecal_identify.c :101
Could not calibrate orders
in [2]sinfo_wavecal_process_chip() at sinfo_wavecal.c :426
Wavelength calibration failed
in [1]sinfo_wavecal() at sinfo_wavecal.c :679
However, the main motivation of this extension is to simplify the error checking and handling. A single line of source code
check( dispersion_relation = sinfo_wavecal_identify(linetable[window-1],
line_refer,
initial_dispersion,
WAVECAL_MODE, DEGREE,
TOLERANCE, ALPHA,
MAXERROR),
"Could not calibrate orders");
has the same effect as
if (cpl_error_get_code() != CPL_ERROR_NONE) {
cpl_msg_error(cpl_func,
"An unexpected error (%s) has occurred "
"in %s() at %-15s :%-3d",
cpl_error_get_message(),
cpl_func,
__FILE__,
__LINE__);
sinfo_free_image(&spectrum);
sinfo_free_image(&cropped_image);
sinfo_free_image(&debug_image);
sinfo_free_cpl(&relative_order);
polynomial_delete(&initial_dispersion);
polynomial_delete(&dispersion_relation);
return NULL;
}
dispersion_relation = sinfo_wavecal_identify(linetable[window-1],
line_refer,
initial_dispersion,
WAVECAL_MODE, DEGREE,
TOLERANCE, ALPHA, MAXERROR);
if (cpl_error_get_code() != CPL_ERROR_NONE) {
cpl_msg_error(cpl_func, "ERROR: Could not calibrate orders (%s) in %s()
at %-15s :%-3d",
cpl_error_get_message(),
cpl_func,
__FILE__,
__LINE__);
sinfo_free_image(&spectrum);
sinfo_free_image(&cropped_image);
sinfo_free_image(&debug_image);
sinfo_free_cpl(&relative_order);
polynomial_delete(&initial_dispersion);
polynomial_delete(&dispersion_relation);
return NULL;
}
This of course makes the source code more compact and hence easier to read (and maintain) and allows for intensive error checking with minimal effort.
Additionally, editing the check()
macro (described below) allows for debugging/tracing information at every function entry and exit.
- Usage
New errors are set with the macros assure()
and passure()
, and sub-functions that might set a cpl_error_code
are checked using the macros check()
and pcheck()
. The function _sinfo_error_set()
should never be called directly. These macros check if an error occured and, if so, jumps to the cleanup
label which must be defined at the end of each function. After the cleanup
label every pointer used by the function is deallocated and the function returns. Also a string variable named fctid
(function identification), must be defined in every function and contain the name of the current function.
At the very end of a recipe the error state should be checked and sinfo_error_dump()
called on error:
if ( cpl_error_get_code() != CPL_ERROR_NONE )
{
sinfo_error_dump(cpl_func);
}
When using this scheme:
- There should be only one
return
statement per function (after the cleanup
label).
- All pointers to dynamically allocated memory must be declared at the beginning of a function.
- Pointers must be initialized to NULL (which is a good idea anyway).
- Pointers must be set to NULL when they are not used (which is a good idea anyway).
Consider the example
int function_name(...)
{
cpl_image * image = NULL;
cpl_image * another_image; / * Wrong: Pointer must be initialized
to NULL. On cleanup,
cpl_image_delete() will try
to deallocate whatever
this pointer points to. If
the pointer is NULL,
the deallocator function
will do nothing. * /
:
:
{
cpl_object * object = NULL; / * Wrong: Pointer must be declared at
the beginning of a function.
This object will not be
deallocated, if the following
check() fails. * /
object = cpl_object_new();
:
:
check( ... );
:
:
cpl_object_delete(object); / * Wrong: The pointer must be set to
NULL after deallocation, or
the following assure() might
cause the already
deallocated object
to be deallocated again. * /
:
:
assure( ... );
return 7; / * Wrong: Only one exit point per
function. * /
}
:
:
cleanup:
cpl_image_delete(image);
cpl_image_delete(another_image);
return 7;
}
This is easily fixed:
int function_name(...)
{
cpl_image * image = NULL; / * All pointers are declared at
the beginning * /
cpl_image * another_image = NULL; / * of the function an initialized
to NULL. * /
cpl_object * object = NULL;
:
:
{
object = cpl_object_new();
:
:
check( ... );
:
:
sinfo_free_object(&object); / * The object is deallocated
and the pointer set to
NULL. * /
:
:
assure( ... );
}
:
:
cleanup:
sinfo_free_image (&image); / * All objects are
deallocated here. * /
sinfo_free_image (&another_image);
sinfo_free_object(&object);
return 7; / * This is the only exit point of
the function. * /
}
(Note that sinfo_free_image()
et al. can be used instead of cpl_image_delete()
et al. as a way to ensure that a pointer is always set to NULL after deallocation).
- Recovering from an error
To recover from an error, call sinfo_error_reset()
, not cpl_error_reset()
. Example:
n = cpl_table_get_nrow(t);
if (cpl_error_get_code() == CPL_ERROR_NULL_INPUT) / * This error code is
set if 't' is NULL.
/
{
/ * Recover from this error * /
sinfo_error_reset();
n = -3;
}
else / * Also check for unexpected errors * /
{
assure( cpl_error_get_code() == CPL_ERROR_NONE, cpl_error_get_code(),
"Error reading table size");
}
However, error recovery is usually best avoided, and the functionality above is better written as:
if (t != NULL)
{
check( n = cpl_table_get_nrow(t), "Error reading table size");
}
else
{
n = -3;
}