in reply to Simple Program Huge Problem

Like ‘almut’ said the DESTROY method doesn’t work as expected so I begin to write a subroutine in the hope of finding a solution to the memory leak problem…
I think I’m almost there however after tracing the execution with ‘Devel::Trace’ I found that the execution order is STEP1; STEP2 and then STEP4, and should be STEP1;STEP2;STEP3.

If the program reached the last return statement why is it going to STEP4 and not to the main function? Could anyone tell me what is going out?

#!/usr/bin/perl -w use strict; use Tree::Simple; #use Tree::Simple 'use_weak_refs'; use Data::Dumper; #use Devel::Trace; $Data::Dumper::Indent=1; #$Devel::Trace::TRACE=0; local $|=1; my $CR="\n"; my $WS="-------------------------------------------------------------- +---$CR"; my @array = qw/ 112.75 850.22 100.65 /; #for (my $j=0;$j<1;$j++) # { #always the same code to execute my $i=0; my $tree = Tree::Simple->new("root tree",Tree::Simple->ROOT); printleaf(\$tree); $tree->addChild(Tree::Simple->new("$array[$i]")); printleaf(\$tree->getChild(0)); my @_auxarray=@array; splice(@_auxarray,$i,1); foreach my $elem (@_auxarray) { printleaf(\Tree::Simple->new("$elem",$tree->getChild(0))); } undef @_auxarray; print "${WS}DATA DUMPER: $CR"; print Dumper $tree; print "${WS}"; FULLY_DESTROY($tree); ###########################STEP 3############################ print "${WS}DATA DUMPER: $CR"; print Dumper $tree; #$tree->traverse(sub { my($_tree)=@_; #print (("\t" x $_tree->getDepth()), $_tree->g +etNodeValue(),"\n"); #}); undef $tree; # } <STDIN>; #pause sub printleaf{ my ($_leaf)=$_[0]; print("VALUE: ",$$_leaf->getNodeValue()," | ROOT?: ",$$_leaf->isRo +ot() ? "true" : "false"," | DEPTH: ",$$_leaf->getDepth()," | INDEX: " +,$$_leaf->getIndex(),"$CR"); } sub FULLY_DESTROY{ my ($self)=$_[0]; print ($self->getNodeValue()," "); while(1) { ###########################STEP 4############################ if(!ref($self->{_parent}) && $self->{_parent} eq 'root' && sca +lar(@{$self->{_children}})==0) { $self->{_parent} = undef; print ("called last to end the sub routine$CR"); ###########################STEP 1######################### +### last; } if(defined($self->{_children}) && $self->getChildCount!=0) { print ("childs ",$self->getChildCount(),"$CR"); FULLY_DESTROY($self->getAllChildren()); } #if has no childs saves the father reference my $ref; my $index=$self->getIndex(); print("index ",$index," "); if(($index+1) == ($self->getParent()->getChildCount())) #last +in the branch { print ("childs 0 last in branch$CR"); $ref = \$self->getParent(); $self->{_parent} = undef; @{$$ref->{_children}}=(); FULLY_DESTROY($$ref); } else { print ("childs 0 not last in branch$CR"); $ref = \$self->getSibling($index+1); $self->{_parent} = undef; FULLY_DESTROY($$ref); } } ###########################STEP 2############################ print "end should return to main function$CR$CR"; return; }

Replies are listed 'Best First'.
Re^2: Simple Program Huge Problem
by crusher (Acolyte) on Nov 09, 2008 at 17:36 UTC
    a call to return after each recursive call, like:
    FULLY_DESTROY($$ref); return;
    solves the problem don't ask me why but it works...

      You seem to be confused about your program flow. From looking at the call structure of your program, the flow will be

      1. Step 1
      2. Step 2
      3. Step 4
      4. Step 3

      You have an endless loop in FULLY_DESTROY, which I don't see where you leave it. Maybe that is the reason for why your program does work differently than you expect it to. You would have found this by removing all other things from your program and replacing it by simple statements that just printed out "STEP 1" etc.