in reply to When to (and not to) use sv_2mortal()
av_push, as documented, takes ownership of one reference count. If you mortalise the scalars, their refcnt will be reduced to zero even though the array still hold a reference to them. Bug. Don't mortalise them.
AV* return_an_array() { AV* av = newAV(); av_push( av, newSViv( 7 ) ); av_push( av, newSviv( 42 ) ); return av; }
The thing is, Perl has a bug of its own. The output typemap for AV*
So the AV and thus its content leak. Avoid it by using the following:
SV* return_an_array() { AV* av = newAV(); av_push( av, newSViv( 7 ) ); av_push( av, newSviv( 42 ) ); return newRV_noinc(av); }
The output typemap for SV* will mortialise the reference.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: When to (and not to) use sv_2mortal()
by davido (Cardinal) on Oct 28, 2011 at 18:14 UTC |