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

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);
    }
  • Comment on Re^2: Memory leak in XS code that handles C++ exceptions