Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2018-06-21 03:29 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.