Hi Monks,

I'm having trouble with a memory leak in XS code that occurs whenever I catch a C++ exception, then croak. I would appreciate your advice as to whether this is a problem with my XS code, or with Perl itself.

I can reproduce the problem in a trivial test library, libfoo:

foo.h ------------------------------------------------------ class Foo { public: void bar(); };
foo.cpp ---------------------------------------------------- #include <stdexcept> #include "foo.h" void Foo::bar() { throw std::runtime_error("oh no!"); }

Here is the XS glue:

Foo.xs ----------------------------------------------------- void Foo::bar() CODE: try { THIS->bar(); } catch (const std::runtime_error &e) { croak("Foo::bar threw a runtime error!"); }

My Perl test script just instantiates Foo and calls bar() in an eval block n times. For n=1000, valgrind reports the following on RHEL5, x64, Perl 5.12.2:

30,969 bytes in 999 blocks are possibly lost in loss record 1,064 of 1 +,069 at 0x4A0666E: operator new(unsigned long) (vg_replace_malloc.c:220) by 0x929F070: std::string::_Rep::_S_create(unsigned long, unsigned +long, std::allocator<char> const&) (new_allocator.h:89) by 0x929FCA4: char* std::string::_S_construct<char const*>(char con +st*, char const*, std::allocator<char> const&, std::forward_iterator_ +tag) (basic_string.tcc:139) by 0x929FE41: std::basic_string<char, std::char_traits<char>, std:: +allocator<char> >::basic_string(char const*, std::allocator<char> con +st&) (basic_string.h:1546) by 0x8FFEABF: Foo::bar() (foo.cpp:6) by 0x8DFC856: XS_Foo_bar (in /home/slower/dev/xstest/src/blib/arch/ +auto/Foo/Foo.so) by 0x49291F: Perl_pp_entersub (in /usr/local/perl/5.12.2-gcc443-rhe +l5-64/bin/perl) by 0x490D35: Perl_runops_standard (in /usr/local/perl/5.12.2-gcc443 +-rhel5-64/bin/perl) by 0x43410B: perl_run (in /usr/local/perl/5.12.2-gcc443-rhel5-64/bi +n/perl) by 0x41E51B: main (in /usr/local/perl/5.12.2-gcc443-rhel5-64/bin/pe +rl) 143,856 bytes in 999 blocks are possibly lost in loss record 1,068 of +1,069 at 0x4A05E1C: malloc (vg_replace_malloc.c:195) by 0x92BF206: __cxa_allocate_exception (eh_alloc.cc:102) by 0x8FFEACD: Foo::bar() (foo.cpp:6) by 0x8DFC856: XS_Foo_bar (in /home/slower/dev/xstest/src/blib/arch/ +auto/Foo/Foo.so) by 0x49291F: Perl_pp_entersub (in /usr/local/perl/5.12.2-gcc443-rhe +l5-64/bin/perl) by 0x490D35: Perl_runops_standard (in /usr/local/perl/5.12.2-gcc443 +-rhel5-64/bin/perl) by 0x43410B: perl_run (in /usr/local/perl/5.12.2-gcc443-rhel5-64/bi +n/perl) by 0x41E51B: main (in /usr/local/perl/5.12.2-gcc443-rhel5-64/bin/pe +rl)

These leaks completely disappear if either 1) Foo::bar doesn't throw an exception, or 2) Foo.xs catches the exception and doesn't croak. I have also confirmed the leak on the following platforms, though valgrind isn't available or doesn't detect it. Watching the memory usage of the test script reveals the leak in these cases (usage increases linearly with the value of n):

The only platform available to me where the leak does not occur is Windows 7, x32, Perl 5.10.0.

Thanks,
m.


In reply to Memory leak in XS code that handles C++ exceptions by slower

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.