This is the mail archive of the
gdb@sourceware.org
mailing list for the GDB project.
Re: Cleanups and Exception handlers
- From: Tom Tromey <tromey at redhat dot com>
- To: Phil Muldoon <pmuldoon at redhat dot com>
- Cc: gdb at sourceware dot org
- Date: Mon, 06 May 2013 11:32:03 -0600
- Subject: Re: Cleanups and Exception handlers
- References: <5180EE37 dot 3020507 at redhat dot com>
>>>>> "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