public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Jason Cipriani" <jason.cipriani@gmail.com>
To: me22 <me22.ca@gmail.com>
Cc: gcc-help@gcc.gnu.org
Subject: Re: try, finally
Date: Wed, 19 Mar 2008 19:09:00 -0000	[thread overview]
Message-ID: <c09049bf0803191202m26988718weacfccc4cf34dcdf@mail.gmail.com> (raw)
In-Reply-To: <fa28b9250803191036v7ed76546u34b92b8321919a53@mail.gmail.com>

> 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

  reply	other threads:[~2008-03-19 19:09 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-19 16:13 Jason Cipriani
2008-03-19 16:14 ` Jason Cipriani
2008-03-19 17:02   ` Tim Prince
2008-03-19 16:37 ` Brian Dessent
2008-03-19 17:31 ` Ted Byers
2008-03-19 17:37 ` me22
2008-03-19 19:09   ` Jason Cipriani [this message]
2008-03-19 19:09     ` me22
2008-03-20  5:50       ` Jason Cipriani
2008-03-20 13:30       ` Noel Yap
2008-03-21  2:27         ` Jason Cipriani
2008-03-19 21:04     ` Ted Byers
2008-03-20  6:28       ` Jason Cipriani
2008-03-20 12:24         ` John Love-Jensen
2008-03-21  2:11           ` Jason Cipriani
2008-03-21  2:37             ` me22
2008-03-21  2:45               ` Jason Cipriani
2008-03-21 15:24             ` Noel Yap
2008-03-21 22:50             ` Brian Dessent
2008-03-21 23:14               ` Noel Yap
2008-03-20 14:50         ` Ted Byers
2008-03-21  2:26           ` Jason Cipriani
2008-03-25 22:28 ` Ian Lance Taylor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c09049bf0803191202m26988718weacfccc4cf34dcdf@mail.gmail.com \
    --to=jason.cipriani@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=me22.ca@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).