library implements an automatic resource cleanup and exception handling
Error handling and cleanup code (at least for code that
error handling) can take quite some space in terms of lines of code to
written. This not only increases the binary size, but makes the code
difficult to read.
Furthermore, error handling code typically lies in untested code paths (because
they get exercised only when abnormal conditions happens) that are very much error
Effective and complete code coverage tests to exercise all error paths, are not trivial
to implement, and very often many of such paths are simply left untested.
has native exception handling, and this solves part of the problem,
keeping exposed the resource cleanup one.
like exception handling, plus automatic resource cleanup, to be used in
software written in
introduces three abstractions, that are
is every object (or action) that needs cleanup. This can be a block of
allocated memory, an open file, a mapped memory region, etc...
is owned by a
can be moved from a
is a bucket inside which
can be the parent of other
by hence introducing a parent/child relationship between
can be reassigned to be child of a new
By freeing a
allocated inside the
will be freed, and all the child
will be recursively freed too.
greatly simplify the resource cleanup code, by being able to issue a
call to free a
and having automatically all the
contained by it, freed as well.
are added/removed in a
(Last In First Out) way, and the
library offers APIs to re-arrange the order of the
is (like in
any kind of abnormal condition that prevent the program to flow in its
This can be a failed memory allocation, a failure to open a file, a
a portion of a file, a failure to
a file, etc...
is described by a unique number
and by an associated data
are thrown using the
XCPC_THROW(ctx, exno, data)
statement, or re-thrown using
A nice feature of exception handling, is that it allows you to handle
certain kind of exceptions, and different exceptions in different
of your code tree.
library, you handle exceptions by surrounding the potentially-throwing
code with the
The code can then use either the
statements to handle specific or all kind of exceptions that happened
code bound by the
block must be terminated by a
If the current
block does not handle the current
statement, and does not have a
library backtrack to find a valid handler in the code at higher layers
the call hierarchy.
It is important that at least the other
block uses a
statement, so that any
not caught by the code, is handled properly.
is caught by an handler, all the resources allocated by the code from
beginning of the
block, down to the place where the
is thrown, are automatically freed once the handler reaches the
(Exception and resource handling in C)
library man page
is available : HTML TXT PDF
The libxcpc library
is made available through the GNU LGPL license
together with the complete sources. Please read carefully the license before
the software. The latest library package is available here :