This is a continuation of Weird memory leak in Catalyst application using Catalyst::Model::KiokuDB, but I've split it into it's own node for clarification and to make it easier for other users to find.

I've found a memory leak in Set::Object-1.28, and using a combination of Test::LeakTrace and Test::Valgrind (thanks, zwon!) I tracked it down to Object.xs. The three test scripts I used to demonstrate and track down the bug is listed in the bottom.

I've made the following patch and submitted it in a bug report at CPAN:

--- Set-Object-1.28/Object.xs 2010-07-14 06:42:11.000000000 +0200 +++ /home/parmus/Projects/Set-Object-1.28/Object.xs 2011-08-04 02:0 +1:56.357000046 +0200 @@ -358,23 +358,8 @@ i--; } if (!c) { - /* we should clear the magic, really. */ - MAGIC* last = 0; - for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) { - if (mg->mg_type == SET_OBJECT_MAGIC_backref) { - if (last) { - last->mg_moremagic = mg->mg_moremagic; - Safefree(mg); - break; - } else if (mg->mg_moremagic) { - SvMAGIC(sv) = mg->mg_moremagic; - } else { - SvMAGIC(sv) = 0; - SvAMAGIC_off(sv); - } - } - last=mg; - } + sv_unmagic(sv, SET_OBJECT_MAGIC_backref); + SvREFCNT_dec(wand); } } }

So now I have two questions for the collective wisdom of the Perl community:

  1. This is my first time with XS and Perl internals. Can anyone here verify the soundness of my patch? Especially, is the any obvious XS or Perl idioms I'm missing?
  2. Apart from finding the bug, making the patch, submitting the bug report and asking the community to validate my findings and my patch (this node), is there any other "How to by a nice Perl hacker"-steps, that I'm missing?

Test scripts used to demonstrate and debug this leak


In reply to Memory leak in Set::Object by parmus

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.