in reply to Re^7: Use of freed value in iteration
in thread Use of freed value in iteration
1) It definitely is a stack-not-refcounted problem. The warning tells you that you are subject to undefined behaviour: that could mean a crash or having your hard disk formatted, as well as just getting the wrong answer. The warning is telling you that the code needs changing, at least for any perl that isn't refcounting stack items.
2) If you change your code to iterate over the keys, then you can check whether the value exists by the time you come to process it, and take whatever avoiding action is appropriate:
my $hr1 = {}; my $hr2 = {}; my $hr3 = {"$hr1" => $hr1, "$hr2" => $hr2}; my @values = sort values %$hr3; print "@values\n"; my (@arr); foreach my $k (sort { $hr3->{$a} cmp $hr3->{$b} } keys %$hr3) { unless (exists $hr3->{$k}) { warn "key '$k' deleted during loop\n"; next; } my $v = $hr3->{$k}; print "$v\n"; delete $hr3->{$values[1]}; push @arr, "hello"; } HASH(0x560669c681e0) HASH(0x560669c683c0) HASH(0x560669c681e0) key 'HASH(0x560669c683c0)' deleted during loop
|
---|