in reply to perlembed: mortalize an AV, get "Attempt to free unreferenced scalar" - don't mortalize, leaks memory
So I'm not supposed to mortalize the scalars that I push onto the array? Why is that? It makes my problem disappear, which I like, but I don't understand it, which I don't like. Any further thoughts are appreciated.
When an object is freed due to its refcount going to 0 all the items that object references have their refcount decremented in turn. So if you stuff an AV with mortalized SV'es the SVes could be freed before the AV itself tries to do the refcount decrement on the scalars it contains. Which then raises a warning as this kind of thing (at least from the perl side point of view) shouldn't happen.
So if you think about what happens in:
my @x; # @x has a refcount of 1 { my @array=(\@x); # @x has a refcount of 2 } # Free lexicals # @x has a refcount of 1
Its a recursive process:
procedure refcount_dec(item) if ( --item.refcount == 0 ) foreach sv in item.contents refcount_dec(sv) endfor if ( item.isref ) refcount_dec(item.references) endif endif endproc
|
---|