Title: RE: Attempt to free unreference scalar?

 
Thanks for your input, Russ.  I have some comments on your comments.  Please see below.

-----Original Message-----
From: Russ Allbery
To: Kort, Eric
Cc: [EMAIL PROTECTED]
Sent: 12/18/2000 6:17 PM
Subject: Re: Attempt to free unreference scalar?

(snip)
>When I ran into this before, it was because I'd not correctly handled
>the reference counts when passing variables into a Perl sub.  It turned
>out that I was "leaking" one reference count each time my code ran, so
>eventually the reference count went to zero despite the fact that the
>variable was still in use.

>I fixed it by finding a place I should have been incrementing and
>wasn't.

After futile web searches and posts to comp.lang.perl.misc, it's great to actually be in contact with someone who has seen this before!  I think you must be right about what is happening here.

>I'm not familiar at all with the Inline module, but is this return:
>
>>   //now get a reference to the copied array and return it
>>   svPtr_arrayRef = newRV_noinc(avPtr_filteredImage);
>>   return svPtr_arrayRef; // return the reference
>
>> }
>
>doing the normal Perl magic stuff?  In other words, is this doing
>something like:
>
>    ST(0) = sv_2mortal(svPtr_arrayRef);
>    XSRETURN(1);
>
>If so, isn't the Perl code going to expect the reference to have
>incremented the reference count of the array that it points to?  In
>other words, why were you using newRV_noinc() here instead of newRV_inc
>()?

According to perlguts, newRV() increments the reference count of the argument, meaning that if the referent already exists (as mine does), the reference count would be two after calling newRV, leading to a memory leak.  Since I don't pass the array itself back from the sub, I want the array destroyed when the reference is destroyed, which it won't be if I increment the reference count to two with newRV() (it will then be lost in space and memory will leak).  Note that I tried using newRV(), but it didn't fix the problem.  Or am I misreading perlguts here?

>I could be on completely the wrong track, in which case maybe I'll learn
>something new too.  :)

I think you/we must be on the right track, but we have yet to ferret out the actual culprit that is not having its reference count handled correctly.  Particularly strange is the fact that it doesn't happen until the 3rd time the sub is called.  Originally this led me to suspect general memory corruption due to memory leakage, but careful observation of the resource meter suggests memory is not leaking and is not being overloaded.

Thanks for your consideration of this problem, and any further insights you may have.

Eric

Reply via email to