bmdhacks has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing some perlxs code that goes something like this:
void add_entry(hash) HV * hash PREINIT: SV * key; SV * val; CODE: key = newSViv( (IV)someint ); val = newSViv( (IV)otherint ); hv_store_ent( hash, key, val, 0 );
This leaks memory. However, if I make the key be a char * and instead use hv_store it does not leak memory. So here's my question: Do I have to free up the SV* key when I'm done using it in hv_store_ent, or is some other error causing my memory leak?

Replies are listed 'Best First'.
Re: hv_store_ent
by rpc (Monk) on Jan 18, 2001 at 01:15 UTC
    This may be your problem.
    From the perlguts manpage:

    Note that both `hv_store' and `hv_store_ent' do not increment the reference count of the stored `val', which is the caller's responsibility. If these functions return a NULL value, the caller will usually have to decrement the reference count of `val' to avoid a memory leak.

    Maybe create the SV with:

    SV *val = sv_newmortal();
    Or mortalize it with:
    sv_2mortal(val);
    Might work? You might need to Mortalize 'key' too.
      Yeah, ignore the non error-checking nature of that sample code, I just did it for brevity. The motality of the 'key' SV is where my question lies. Can anyone verify that I need to kill it (or decrement it's reference count) after passing it to hv_store_ent?
Re: hv_store_ent
by bmdhacks (Novice) on Jan 18, 2001 at 03:09 UTC
    I changed the code to look like this:
    void add_entry(hash) HV * hash PREINIT: SV * key; SV * val; CODE: key = sv_newmortal(); sv_setiv( dt, (IV)someint ); val = newSViv( (IV)otherint ); hv_store_ent( hash, key, val, 0 );
    The memory leaks went away now, and my hash doesn't seem to be getting corrupted. I'd still like somebody to verify the correctness of this though.

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