Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#1134 C++ memory leak of temporaries on exception

open
nobody
perl (97)
5
2012-12-21
2011-02-23
Roy LeCates
No

Temporary C++ objects (created as "new local variables" in a typemap) do not get destroyed properly when SWIG_fail is used. In the generated wrapper, the "fail" label is incorrectly located in the same scope as the temporary variables. The SWIG_fail jumps to "fail:" which triggers SWIG_croak_null() which results in a Perl croak() which uses longjmp() to exit. Hence, the destructors for objects on the stack are never called and associated memory is leaked. A simple fix is to place the entire function contents (before the "fail" label) inside of a { } block, causing it to leave scope cleanly before croaking.

For example, use of the following typemap will cause temp_str to be leaked on a SWIG_fail.
%typemap(in) String (std::string temp_str) {
STRLEN temp_len = 0;
const char* temp_ptr;
temp_ptr = SvPV($input, temp_len);
temp_str.assign(temp_ptr, temp_len);
$1 = &temp_str;
}

Discussion