newHV
returns an HV
with a reference count (refcnt) of one, signifying your code's hold on that HV
. When you're done with that HV
, you must release your hold on it by decrementing its refcnt. There are three common ways of doing this.
Done with it here and now.
SvREFCNT_dec((SV*)hv);
// hv is no longer safe to use here.
AV
and HV
are "subclasses" of SV
.
Done with it after the caller has a chance to reference it. (Doesn't really apply to hashes.)
return sv_2mortal(sv);
Transfer "ownership".
rv = newRV_noinc((SV*)hv);
That's short for
rv = newRV((SV*)hv);
SvREFCNT_dec((SV*)hv);
Note that you must similarly release your hold on the rv
when you're done with it, so you'll often see the following:
return sv_2mortal(newRV_noinc((SV*)hv));