Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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 http://msdn.microsoft.com/en-us/library/yz2ez4as%28v=vs.90%29.aspx?

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
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.

    From http://en.cppreference.com/w/c/program/longjmp
    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:
    void
    Foo::bar()
      CODE:
        const char* error = 0;
        try {
          THIS->bar();
        }
        catch (const std::runtime_error &e) {
          error = e.what();
        }
    
        if (error != 0) {
          croak(error);
        }
    

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2015-07-06 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (74 votes), past polls