This is a follow-up on djantzen's post Depth First Search through Digraph Results in Memory Leak.

I played around and simplified the program as far as possible, ending up at the following:

use strict; use warnings; sub UNIVERSAL::DESTROY { warn "DESTROYING @_\n"; } sub dfs { my ($node, $sub) = @_; my %visited; my $dfs; $dfs = sub { my ($n) = @_; $visited{$n}++; $sub->($n); for (@$n) { next unless ref; $dfs->($_->[0]) unless $visited{$_->[0]}; } }; $dfs->($node); } sub do_dfs { my ($node) = @_; my $nodes = []; my $search = sub { push @$nodes, $_[0] }; dfs($node,$search); # UNCOMMENT NEXT LINE to see bug # return $nodes; my @nodes = @$nodes; undef $nodes; return [@nodes]; } warn "STARTING\n"; { my $node1 = bless [1], "Node"; my $node2 = bless [2], "Node"; push @$node1, bless [$node2], "Link"; { my $nodes = do_dfs($node1); warn "Neighbors\n"; warn " $_\n" for (@$nodes); } } warn "SHOULD BE THE LAST THING PRINTED.\n";

This gives the following expected output:

STARTING Neighbors Node=ARRAY(0x8148a8c) Node=ARRAY(0x8148b94) DESTROYING Node=ARRAY(0x8148a8c) at segfault.pl line 6. DESTROYING Link=ARRAY(0x818fc00) at segfault.pl line 6. DESTROYING Node=ARRAY(0x8148b94) at segfault.pl line 6. SHOULD BE THE LAST THING PRINTED.

However, uncommenting the indicated line, leads to the follwing output and a segfault.

STARTING Neighbors Node=ARRAY(0x8148a8c) Node=ARRAY(0x8148b94) SHOULD BE THE LAST THING PRINTED. DESTROYING Node=ARRAY(0x8148a8c) at segfault.pl line 6 during global d +estruction. DESTROYING Link=ARRAY(0x818fc48) at segfault.pl line 6 during global d +estruction. DESTROYING Node=ARRAY(0x8148b94) at segfault.pl line 6 during global d +estruction. DESTROYING IO::Handle=IO(0x818fc6c) at segfault.pl line 6 during globa +l destruction. DESTROYING IO::Handle=IO(0x815c018) at segfault.pl line 6 during globa +l destruction.

I'm on Linux running perl 5.8.0. Can someone confirm with other versions of perl?

Update
OK, summing up the results other people got, the follwoing perls segfault

and these perls do not segfault:

What is still consistenly wrong is destruction order. The objects referenced in $node1 and $node2 should be destroyed on block exit not during global destruction, shouldn't they?

-- Hofmator


In reply to Perl segfault and global destruction problem by Hofmator

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.