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.