perl-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Geoffrey Young <ge...@modperlcookbook.org>
Subject Re: possible pnotes refcounting bug ?
Date Thu, 11 Oct 2007 13:57:11 GMT


Torsten Foertsch wrote:
> On Thursday 11 October 2007 15:18, Torsten Foertsch wrote:
>>     if (key) {
>>         STRLEN len;
>>         char *k = SvPV(key, len);
>>
>>         if (val) {
>>             retval = *hv_store(*pnotes, k, len, SvREFCNT_inc(val), 0);
>>         }
>>         else if (hv_exists(*pnotes, k, len)) {
>>             retval = *hv_fetch(*pnotes, k, len, FALSE);
>>         }
>>     }
>>     else {
>>         retval = newRV_inc((SV *)*pnotes);
>>     }
>>
>>     return retval ? SvREFCNT_inc(retval) : &PL_sv_undef;
>>
>> I am wondering whether the REFCNT is always right. *pnotes is a HV. If the
>> function is called without a key argument the else branch newRV_inc
>> increments the REFCNT of the HV, right? Then the return statement in the
>> last line increments it again? Am I wrong?
> 
> I think it should rather read:
> 
>     if (key) {
>         STRLEN len;
>         char *k = SvPV(key, len);
> 
>         if (val) {
>             retval = *hv_store(*pnotes, k, len, SvREFCNT_inc(val), 0);
>         }
>         else if (hv_exists(*pnotes, k, len)) {
>             retval = *hv_fetch(*pnotes, k, len, FALSE);
>         }
> 
>         return retval ? SvREFCNT_inc(retval) : &PL_sv_undef;
>     }
>     else {
>         return newRV_inc((SV *)*pnotes);
>     }

again, I'll defer to philippe :)

but it seems to me that gives

  my $pnotes = $r->pnotes();

a refcount of one.  then if someone

  $pnotes->($key => $val);

$pnotes still has a refcount of one, so when the handler exits perl will
clean it up and the next handler to ask for pnotes() won't have the new
value.  adding one to the count means that the internal representation
won't get cleaned up by perl behind the scenes and muck with what we
expect pnotes to do.

but, in truth, it's been ages since I worked closely with this stuff,
and at my age the mind starts to go rather quickly, so I could be
completely off base :)

--Geoff

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Mime
View raw message