On Thu, Nov 28, 2002 at 04:30:41PM +0000, Shevek wrote: > On Thu, 28 Nov 2002, Nicholas Clark wrote: > > > On Thu, Nov 28, 2002 at 11:45:13AM +0000, Richard Clamp wrote: > > > > [someone check this, please:] > > 3: so this means if you're returning a reference to an array or hash from > > your XS code, you only need to mark the top level reference as mortal; when > > that things reference count drops to zero, everything it owns is dropped > > by one. (I believe that this means the example on page 366 of Advanced > > Perl Programming is wrong - it makes everything it pushes into an array > > mortal) > > IANAL, but doesn't making it mortal mean that it does not have a reference > "for you", as it were, i.e. it will be freed at the next FREETMPS, which > is correct, since if you're pushing it into an array, you don't want a > reference to it. Presumably (IANAL again) the push operation will add a > reference, so it now has 1, which is correct.
What's L in this case? I can however say that your presumptions are wrong. When you add things to an AV by pushing it nothing ups up the refcount of them for you. av_push is implemented in terms of av_store, which has the following note in perlapi. av_store Stores an SV in an array. The array index is specified as "key". The return value will be NULL if the operation failed or if the value did not need to be actually stored within the array (as in the case of tied arrays). Otherwise it can be dereferenced to get the original "SV*". Note that the caller is responsible for suitably incrementing the reference count of "val" before the call, and decrementing it if the function returned NULL. The last sentence is the kicker. I guess that maybe you're thinking of returning things on the stack, whereby you want to mortalise them, then your caller will up the refcount when it takes the copies of the values[1] if it assigns the values to something. So that my_sub(); # the mortalised SVs die @foo = my_sub(); # they live on within @foo; [1] Unsurprisingly for perl it actually cheats and takes the entire SV if it sees that it's mortal and about to disappear rather than copying the structure about. Yet another Cunning Optimisation. -- Richard Clamp <[EMAIL PROTECTED]>