Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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]
[Happy-the-monk]: (purely coincidental)
[jkva]: Corion Hey there :)
[Happy-the-monk]: Discipulus: are there any perl monks in Milano?
[Discipulus]: i the moment likely no
[karlgoethebier]: i wonder now it will be with my glasses.
[Discipulus]: Milan Perl Mongers seems frozen at 2009
[Discipulus]: karl you dont shoot with eyes, you shoot with the soul
[jkva]: Reminds me of Guardians of the Galaxy

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2017-10-20 10:04 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (260 votes). Check out past polls.