I repeatedly do "hv_store(self, "hashslice", sizeof("hashslice")-1, newRV_inc(someSV), 0);"

Isn't sizeof("hashslice")-1 one less than the size of a pointer? You want strlen("hashslice") (no -1).

Will hv_store run SvREFCNT_dec on it?

Yes.

I see the refcnt of sameSV going up and up.

If you keep assigning to the same key, it'll go up when you create the reference, then go back down when the previous reference you created gets freed as hv_store overwrites the old value. Unless you copied the old reference.

my $x; for (1..3) { $h{hashslice} = \$x; print(Internals::SvREFCNT($x), "\n"); }
2 2 2
my $x; my @a; for (1..3) { $h{hashslice} = \$x; print(Internals::SvREFCNT($x), "\n"); push @a, $h{hashslice}; }
2 3 4

The examples of hv_store never capture the returned SV ** or do anything with

The docs say "Note that the caller is responsible for suitably incrementing the reference count of val before the call, and decrementing it if the function returned NULL." so your code should look like:

if (!(sv = hv_store(...))) { SvREFCNT_dec(sv); ... }

In reply to Re: hv_store and memory leaking/ref counts by ikegami
in thread 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.