in reply to Re: hv_store_ent
in thread hv_store_ent

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")

Replies are listed 'Best First'.
Re: (tye)Re: hv_store_ent
by bmdhacks (Novice) on Jan 21, 2001 at 03:37 UTC
    You are a saint! So it looks like hv_store_ent does not increment the reference count of the key SV* so I'm assuming it copies it at some level. Does anyone know of a channel I can use to suggest a clarification of this in the perlguts manpage? Thanks for the help.
      Patch your local copy of the documentation and then send the patch (diff -u please, if you do not have a version of diff that does this you can get it here) email to perl5-porters@perl.org.