The only purpose of "mortal" status is to allow "temporary" variables to stay alive long enough to get popped off the stack. You aren't putting these on the stack so mortalization isn't really the correct answer here.

When you create the SV, it starts out with a ref count of 1. You can look at this two ways: 1) either that your SV * variable is that 1 reference and you need to decrement the reference count before you return -or- 2) that the reference count is too high and you need to make something refer to the new SV (w/o incrementing the ref count) or you'll get a memory leak.

I tend to think of it the other way. If the only thing you do with the SV is push it on the stack, then you need to make it a mortal.

It appears (though the docs weren't completely clear on this to me) that hv_store_ent() increments the ref count of 'key' is but not the ref count of 'val'. So you need to decrement the ref count on 'key'. Making it mortal does a delayed ref count decrement, so this mostly works. But the more proper solution is simply to decrement the ref count after the call to hv_store_ent() via SvREFCNT_dec(key) (or decement the ref count at the point where 'key' will no longer point to that SV *, that is, just before you return, which is the same point in this case).

If it were my code, I'd test the above assumptions once by temporarilly adding code to look at SvREFCNT(key) and SvREFCNT(val) after each operation on those variables.

        - tye (but my friends call me "Tye")

In reply to (tye)Re: hv_store_ent by tye
in thread hv_store_ent by bmdhacks

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.