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")In reply to (tye)Re: hv_store_ent
by tye
in thread hv_store_ent
by bmdhacks
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |