Cleanups and Exception handlers

Tom Tromey tromey@redhat.com
Mon May 6 17:32:00 GMT 2013


>>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes:

Phil> I'd like to quantify and discuss strategies of cleanups and GDB exception
Phil> handlers.  It seems I am always making mistakes in this area, and the
Phil> comments in the TRY_CATCH macro (well to me) are not adequate about the
Phil> mechanics of cleanups in an exception handler.

Phil> So I would like to discuss patterns of usage with a view to updating
Phil> the comments to be more explanatory.

There's also a section in gdbint.texinfo, though it seems reasonably out
of date, seeing as it does not mention TRY_CATCH at all.


The simplest, and IMO therefore best, way to approach cleanups is to
pretend that they introduce blocks.

That is, when you see an assignment of a cleanup to a local:

    cleanup = make_cleanup (...);

you should mentally add a "{" to the text.

And when you see a do_cleanups or discard_cleanups call, you should
mentally add a "}".

Then, if the braces in the function -- all of them, the real ones plus
the one you added mentally -- do not balance, something is wrong.


This approach is sufficient for most of the code in gdb.  There are some
necessary exceptions to the rule (some functions must return cleanups
somehow; and also sometimes the strict lexical rule will not work), some
weird code, and some code making assumptions about its caller.  The
cleanup checker (archer.git tromey/cleanup-checker) diagnoses these.


It is of course possible to be more dynamic with cleanups and not to
pretend they are block structured.  There aren't any actual rules.
However, I think the more dynamic style is bug-prone, and my proof of
this is the large number of actual bugs in this area that I've fixed
over the years, including all the ones fixed on the cleanup-checker
branch but not yet submitted.

Tom



More information about the Gdb mailing list