Kort, Eric <[EMAIL PROTECTED]> writes:

> 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?

Ah... no, that makes sense to me.  Okay.  Hm.

I was assuming that the use Inline bit was handling the magic of taking a
C return value and translating it into a Perl stack return; is that
correct?  Normally I'm used to having to mark stack return values as
mortal, which also plays around with the reference count.

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

This is the same thing that happened when I ran into this error; as near
as I could tell, Perl doesn't actually notice an inconsistent reference
count until it gets several counts off, so a slow "leak" of reference
counts doesn't show up until after multiple calls.

The place where I was having this problem before was rather different than
what you're running into; I was storing a value directly into a Perl SV
from C code.  That code looks like:

    if (artBody) {
        if (!body) {
            body = newSV(0);
            (void) SvUPGRADE(body, SVt_PV);
        }
        SvPVX(body) = artBody;
        SvCUR_set(body, strlen(artBody));
        SvLEN_set(body, 0);
        SvPOK_on(body);
        (void) SvREADONLY_on(body);
        (void) SvREFCNT_inc(body);
        hv_store(hdr, "__BODY__", 8, body, 0);
    }

followed later by:

    hv_undef(hdr);

and if I left off the SvREFCNT_inc, IIRC, I was getting the same error.

-- 
Russ Allbery ([EMAIL PROTECTED])             <http://www.eyrie.org/~eagle/>

Reply via email to