Do you know also why the segfault is produced first if the script die and not immediate?
The stack-overflow is triggered when the top-level hash goes out of scope. In your example that is when the script ends, but that's just coincidental. If your hash goes out of scope before that, and your stack-size isn't big enough, you'll get a segfault at that point:
{
my $i = 60000;
my %h = (new => {});
my $r = \%h;
for (0..$i) {
print $_, " ";
$r = $r->{new};
$r->{new} = {};
}
print "end 1\n";
} # <-- hash goes out of scope; segfault if stacksize is too low
print "end 2\n";
I'm not familiar with the implementation of perl's garbage collector, but I've seen this problem once before. In that case as in this one, the problem was caused by a program letting a long linked list (i.e. item a has a reference to item b which has a reference to item c etc. etc.) go out of scope.
If you're really careful, you can clean up the linked list before it goes out of scope and prevent this error:
{
my $i = 60000;
my %h = (new => {});
my $r = \%h;
my $end = $r; # end of the list
for (0..$i) {
print $_, " ";
$r = $r->{new};
$r->{new} = {};
$r->{new}->{old} = $r; # keep back-references
$end = $r->{new};
}
print "end 1\n";
while ($end) { # delete the list from the end
delete $end->{new};
$end = delete $end->{old};
}
}
print "end 2\n";
|