in reply to Re^8: Use of freed value in iteration
in thread Use of freed value in iteration

It fixed that particular demo. The script output became: aaa bbb aaa bbb
If $_[0] is supposed to be an alias of $a[0] and $_[1] an alias of $a[ +1], I'm not sure I'd want that second print "@_\n" to print aaa bbb.

At least in a loop, if an object has been deleted I'd want that to be noticed when trying to access it. It seems like the desirable behavior, regardless of what the refcount is doing, would be for 1) deleted values occurring within the loop to be reflected if those values are accessed, but 2) the memory occupied by those values couldn't be reused by a different object during the loop. This may be desirable for the subroutine behavior also.

Replies are listed 'Best First'.
Re^10: Use of freed value in iteration
by dave_the_m (Monsignor) on Feb 25, 2024 at 12:16 UTC
    I'm not sure I'd want that second print "@_\n" to print aaa bbb.
    I'd want it to. Consider this:
    my @a = qw(aaa bbb); my $r = \$a[0]; @a = (); print $$r;
    I'd expect that to print "aaa". This is pretty much at the heart of the reference-counting model at the heart of perl. Similarly to how the fact that the lexical var getting freed on scope exit doesn't free the return value in:
    sub f { my $x = ...; return $x }

    Dave.

      "This is pretty much at the heart of the reference-counting model at the heart of perl."
      I see what you mean. So do you think that when iterating over values, the refcount of all values being iterated should have their refcount incremented before the loop starts?
        So do you think that when iterating over values, the refcount of all values being iterated should have their refcount incremented before the loop starts
        Under PERL_RC_STACK builds (which will eventually become the default), this will happen automatically, as all items will get their reference count incremented when pushed onto the the argument stack (which doesn't happen at the moment). A for loop by default just iterates over a range of items on the stack.

        Dave.