Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^8: Use of freed value in iteration

by syphilis (Archbishop)
on Feb 14, 2024 at 00:11 UTC ( [id://11157684]=note: print w/replies, xml ) Need Help??


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

That seems like a good feature instead of a problem.

dave_the_m has done (and continues to do) quite a bit of work on refcounting the stack.
You can check on the progress by building blead with the -DPERL_RC_STACK added to ccflags.
IIRC, on linux builds, just add -Accflags=-DPERL_RC_STACK to the Configure args.
Here's a simple demo of the issue, posted a while back by Dave (on the p5p mailing list, I think):
@a = qw(aaa bbb); f(@a); sub f { # on entry, $_[0] is an alias of $a[0], # $_[1] is an alias of $a[1], print "@_\n"; # correctly prints "aaa bbb" @a = (); # this prematurely frees $_[0] and $_[1] # this causes the two just-freed SVs to be reallocated my $x = 'xxx:yyy'; my @x = split /:/, $x; # but the two reallocated SVs are still referenced by @_ print "@_\n"; # incorrectly prints "xxx yyy" }
Four months back, I built the then-current devel release (perl-5.39.3) with that flag added - just curious to see whether it did something it shouldn't be doing on Windows. (It was fine, of course.)
It fixed that particular demo. The script output became:
aaa bbb aaa bbb
But there are still other aspects of the issue to be addressed, upon which Dave continues to work.

Cheers,
Rob

Replies are listed 'Best First'.
Re^9: Use of freed value in iteration
by Danny (Pilgrim) on Feb 24, 2024 at 21:48 UTC
    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.

      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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11157684]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-05-29 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found