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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |