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

Yes or no? Perlguts and perlapi don't say. All they say is that hv_store() and company don't increment the refcount of what gets stored.

I ask because I have a bit of code where one routine creates a scalar and adds it to a hash, and another deletes and destroys that scalar. After the call to hv_delete(), I call SvREFCNT_dec() on the SV * returned, and get an error for trying to decrement the refcount of scalar whose refcount *already* dropped below one. Comment out the line and it works fine. Which leads me to think hv_delete() is decrementing the refcount already for me. Is this true?

  • Comment on Does hv_delete() decrement the reference count of the SV * it returns?

Replies are listed 'Best First'.
Re: Does hv_delete() decrement the reference count of the SV * it returns?
by tachyon (Chancellor) on Oct 14, 2004 at 03:00 UTC

    Yes. It is documented, you just have to RTFS ;-) If you take a quick glance at the source in hv.c you will see that Perl_hv_delete (hv_delete is a define in objXSUB.h) calls Perl_hv_free_ent which in turn calls SvREFCNT_dec

    cheers

    tachyon

      Thanks tachyon!

      Not to nitpick, but don't you think this is a little unacceptable? If this reference counting scheme is going to work, don't the docs have say which routines mess with their arguments refcounts and which don't?

      Is there anyway I could get this info added to perlapi and perlguts? I've found other inconsistencies and omissions in the Perl C documentation too.

        Perl 5 guts are inconsistent and perl's ref counting is somewhat sub optimal, this is part of the desire for a total overhaul. If you wanted to submit updates to the documentation you would send them as patches to p5p. Here are the patching guidelines Perlguts.pod is a real doc in the pod/ dir. Perlapi.pod is autogenerated by embed.pl. NB: embed.pl also autogenerates perlapi.c Read the header of perlapi.c to find the correct files to edit that eventually become perlapi.pod.

        cheers

        tachyon