At 12:08 PM 3/8/2001 -0500, Kort, Eric wrote:
> > 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"?

Yup. You get to say hi to core dumps and bizarre inconsistent behaviour, 
and that's just *so* much fun... :)

>
> > 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).

Because you're incrementing the refcount after. When you stick an SV in an 
array, it puts the SV in the array. You're not making a copy or anything, 
so this:

>           svItem = *av_fetch(avArray, i, 0);
>           av_push(avNewArray, svItem);
>           svItem = SvREFCNT_inc(svItem);

really does affect the SV you pushed into the array. (You should really 
only increment the refcount if the push returned a non-NULL result, since 
the push can fail if you blow out of memory)

                                        Dan

--------------------------------------"it's like this"-------------------
Dan Sugalski                          even samurai
[EMAIL PROTECTED]                         have teddy bears and even
                                      teddy bears get drunk

Reply via email to