> -----Original Message-----
> From: Dan Sugalski [mailto:[EMAIL PROTECTED]]
> At 11:21 AM 3/8/2001 -0500, Kort, Eric wrote:
> >Bingo. If I mortalize, the problem is (almost entirely) resolved:
> >
> >av_push(avMask, sv_2mortal(*av_fetch(avImage, i*3, 0)) );
>
> Ack! Do *not* do that. Bad, bad, bad! What that'll do is have
> the scalars
> in the array freed the next time temps are cleaned up. If you
> try and use
> them later, Odd Things Will Happen.
Heh...classic "Out of the frying pan into the fire"?
> Instead, increment the refcounts for the scalars you store that way.
> av_fetch does *not* increment the refcounts for the scalars
> it returns, and
> av_push doesn't increment the refcounts of scalars it sticks
> in the array.
Ok, so in the following example (which does not leak--see test output at
end), I increment the reference count of the SV* I push onto the array.
However, I do not increment the reference count following av_fetch. Why
don't I need to increment after av_fetch (I conclude I do not need to
because I am not leaking memory).
SV* stopLeak(SV* svArray)
{
AV *avArray, *avNewArray;
int i, iArrayL;
SV *svItem;
avNewArray = newAV();
svItem = newSV(0);
avArray = (AV*)SvRV(svArray);
iArrayL = av_len(avArray);
for (i=0; i<iArrayL; i++)
{
svItem = *av_fetch(avArray, i, 0);
av_push(avNewArray, svItem);
svItem = SvREFCNT_inc(svItem);
}
printf("Ref counts: avArray: %d, avNewArray: %d, svItem: %d\n",
SvREFCNT((SV*)avArray), SvREFCNT((SV*)avNewArray),
SvREFCNT(svItem));
return newRV_noinc((SV*)(avNewArray));
}
Here is output from my "test harness":
----------------------------------------------------------------------------
-
Cycle 46: Ref counts: avArray: 1, avNewArray: 1, svItem: 2
total used free shared buffers cached
Mem: 127916 61200 66716 13480 21572 19940
----------------------------------------------------------------------------
-
Cycle 47: Ref counts: avArray: 1, avNewArray: 1, svItem: 2
total used free shared buffers cached
Mem: 127916 61200 66716 13480 21572 19940
----------------------------------------------------------------------------
-
Cycle 48: Ref counts: avArray: 1, avNewArray: 1, svItem: 2
total used free shared buffers cached
Mem: 127916 61200 66716 13480 21572 19940
----------------------------------------------------------------------------
-
Cycle 49: Ref counts: avArray: 1, avNewArray: 1, svItem: 2
total used free shared buffers cached
Mem: 127916 61200 66716 13480 21572 19940
----------------------------------------------------------------------------
-