Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Memory leak in XS code that handles C++ exceptions

by bulk88 (Priest)
on Oct 03, 2013 at 02:31 UTC ( #1056710=note: print w/replies, xml ) Need Help??

in reply to Memory leak in XS code that handles C++ exceptions

Does your unix longjmp() call C++ destructors (some do, see

croak is implemented with longjmp. Would you call longjmp from a catch block if this wasnt Perl? The simplest longjmp implementation will make the C++ exception dispatcher leak all of its resources.

WAG, do a goto from the catch block to the parent block (outside of try/catch), then call croak. Or try a finally block.
  • Comment on Re: Memory leak in XS code that handles C++ exceptions

Replies are listed 'Best First'.
Re^2: Memory leak in XS code that handles C++ exceptions
by slower (Acolyte) on Oct 03, 2013 at 14:53 UTC
    Thanks bulk88, I wasn't aware of the internal implementation of croak, or longjmp for that matter. I see that longjmp combined with C++ objects can be a Very Bad Thing:
    No destructors for automatic objects are called. If replacing of longjmp with throw and setjmp with catch would execute a non-trivial destructor for any automatic object, the behavior of such longjmp is undefined.

    Your suggestion to croak outside the catch block worked! I actually don't even need a goto; I can just capture the exception message in a string, let the catch block exit normally, then croak with the captured message like so:
        const char* error = 0;
        try {
        catch (const std::runtime_error &e) {
          error = e.what();
        if (error != 0) {

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1056710]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (13)
As of 2016-10-27 18:47 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (369 votes). Check out past polls.