I am tracking down a memory leak in XS code I wrote. I repeatedly do "hv_store(self, "hashslice", sizeof("hashslice")-1, newRV_inc(someSV), 0);" on the hash. someSV is the same pointer every time the hv_store runs. I see the refcnt of sameSV going up and up. hv_store may or may not be the source of my memory leak, (I have a few other ideas) but I'm confused on how to use hv_store, so I want to be dead certain I'm using it correctly.

What happens if the hash slice has an existing SV in the slice when you run hv_store on that hash slice? Will hv_store run SvREFCNT_dec on it? or will the existing SV appear in the return of hv_store (as an SV **)? or do you have to explicitly run hv_fetch and then possibly hv_delete before you can safely do hv_store?

The documentation doesn't say anything. In perlguts it just says hv_store returns an SV **. The examples of hv_store never capture the returned SV ** or do anything with it. In perlxstut] hv_store is used on a virgin HV, so no SV can be in that slice when we call hv_store. In perlapi it says "The return value will be NULL if the operation failed or if the value did not need to be actually stored within the hash (as in the case of tied hashes). Otherwise it can be dereferenced to get the original SV* ." and it talks about the refcounts for the SV your going store at the hash slice. Not about the refcounts of what used to be/currently is in the hash slice.

In reply to hv_store and memory leaking/ref counts by patcat88

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.