This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: try, finally


> On Wed, Mar 19, 2008 at 12:13 PM, Jason Cipriani
>  <jason.cipriani@gmail.com> wrote:
>
> > Does GCC have anything similar to the MS and Borland compiler's __try
>  >  and __finally keywords? When using GCC I often find that I have code
>  >  like this (a moderately complex, and highly contrived, example):

Thanks for your responses, guys.

On Wed, Mar 19, 2008 at 12:37 PM, Brian Dessent <brian@dessent.net> wrote:
>  Adding SEH support to gcc has been tossed around for years but nothing
>  usable has yet to come of it.  You can still use SEH through
>  SetUnhandledExceptionFilter() or by manipulating the exception chain at
>  %fs:0 manually, but there is no built in compiler support for it.

Oh well. Maybe some day. Thanks for the tips. I probably won't do
anything like messing with SetUnhandledExceptionFilter(), mostly I
just want to simplify code in common situations, I don't want to do
anything *too* strange, though.

On Wed, Mar 19, 2008 at 1:02 PM, Tim Prince <tprince@myrealbox.com> wrote:
>  The w32api headers in cygwin replicate some of this functionality.

Thanks. I'll check those out just to see how they do it, but I'm
actually doing Linux development at the moment, and on Windows I
usually use MinGW GCC.

On Wed, Mar 19, 2008 at 1:30 PM, Ted Byers <r.ted.byers@rogers.com> wrote:
>  What you say suggests you ought to take an hour or two
>  to study RAII (resource acquisition is
>  initialization), and see how far that allows you to
>  clean up obviously problematic code.

Agreed; I know the idiom but I've been always avoided the std and
boost RAII utilities for some reason -- however, it's 2008 and
probably about time for me to bite the bullet, especially with C++0x
coming out some day, I have no excuse.

One situation I frequently find myself in is something like: "Ugh... I
want to write this code and I really don't feel like implementing
everything required to make resource cleanup automatic here; I'll just
put cleanup code everywhere instead". In reality, those things are
already written, I guess.

me22 <me22.ca@gmail.com> wrote:
> Why bother with void* and C?

Just an example.

So, WRT what Ted Byers and me22 said, an issue I typically have that I
always assumed the std / boost utilities couldn't handle (and hence
one reason why I never bother looking into them), is that it's not
always as simple as creating objects with new and delete. For example,
some code I recently wrote (and what prompted me to ask this question)
used libxml2, a C library, from my C++ code; say something like this
(if you are actually familiar with libxml2; I made up the
FindChildNode function):

=====

xmlDoc *doc = NULL;
xmlNode *root, *node;
xmlChar *str1 = NULL, *str2 = NULL, *str3 = NULL;

// load document, fail on error
if (!(doc = xmlReadFile(...)))
  throw Something();

try {

  // get root xml node, fail if document is empty
  if (!(root = xmlDocGetRootElement(doc)))
    throw Something();

  // get "first" node string into str1, fail on error
  if (!(node = FindChildNode(root, "first")))
    throw Something();
  if (!(str1 = xmlNodeGetContent(node)))
    throw Something();

  // get "second" node string into str2, fail on error
  if (!(node = FindChildNode(root, "second")))
    throw Something();
  if (!(str2 = xmlNodeGetContent(node)))
    throw Something();

  // get "third" node string into str3, fail on error
  if (!(node = FindChildNode(root, "third")))
    throw Something();
  if (!(str3 = xmlNodeGetContent(node)))
    throw Something();

  // do something with str1, str2, str3

} catch (...) {

  xmlFree(str3);
  xmlFree(str2);
  xmlFree(str1);
  xmlFreeDoc(doc);
  xmlCleanupParser();
  throw;

}

// and duplicate cleanup code:
xmlFree(str3);
xmlFree(str2);
xmlFree(str1);
xmlFreeDoc(doc);
xmlCleanupParser();

=====

Now, in this specific example: there are a few other third-party
libraries available that provide C++ bindings to libxml2 -- let's say
that for whatever reason they are not acceptable here. And in the
general case, the cleanup code here is not "deleting" something, they
are custom cleanup functions that I have no control over.

If my goal is to reduce the amount of coding I have to do, it is far
easier for me to duplicate the cleanup code than to go and write a
bunch of small C++ wrapper objects with automatic cleanup that I can
use interchangeably with the libxml2 data types.

Does std or boost have some kind of "smart pointers" that let me
define the cleanup actions without having to write too much additional
code?

Thanks,
Jason


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]