I have a class that includes an iterator method for outputing in a stream-like fashion. (tye's response in this thread shows pretty much how the code is constructed Re: Streaming to Handles (iterator)) Since this I've slightly amended the code to include some other "features". It all works beautifully although I've noticed it now leaks memory. I believe I've located the source of the memory leak although am not sure how to retain the features while effectively removing the memory leak. The below code highlights the problem.

I push an array reference to the $self->{files} array by using the below code:
my @array = (); push (@array, $rel_path); ## in my code more than one thing is pushed to @array. push (@{$self->{files}}, \@array);
(I'm sure that's where the problem lies!)

In the "next" iterator method I then "return" these references from the $self->{files} array:
sub next { my( $self )= @_; while( 1 ) { if( @{ $self->{files} } ) { my $file = shift @{ $self->{files} }; ## HERE! if( -d $file ) { push @{ $self->{dirs} }, $file; } return $file; } if( ! @{ $self->{dirs} } ) { return; } my $dir= shift @{ $self->{dirs} }; if( opendir( DIR, $dir ) ) { $self->{files}= [ map { File::Spec->catfile( $dir, $_ ); } File::Spec->no_upwards( readdir(DIR) ) ]; closedir DIR; } else { warn "opendir failed, $dir: $!\n"; } } }
... which I then dereference in my calling script:
my $file; while( $file = $f->next() ) { print "@{$_}\n"; }
Now I can see this method of pushing array references is leaving a reference count against the @array array ... and hence @array is never destroyed. This in time means RAM gets munched and for a long running script eventuates in my computer dying. But I need the feature of pushing a set of values (possbily as a array unless there is an alternative) into the $self->{files} array which I can then recall together as set

Is there a way to achieve this without leaving a reference count against @array (and hence removing the memory leak) ?

Does all of that make sense?

UPDATE: After writing this I layed down in bed and a thought suddenly struck me - Instead of making an @array to reference why not just push an anonymous array?
push (@{$self->{output}}, [$rel_path, $var1, $var2]);
I'll have to test this tomorrow but if anyone wishes to comment then please do so. :-)

Dean
The Funkster of Mirth
Programming these days takes more than a lone avenger with a compiler. - sam
RFC1149: A Standard for the Transmission of IP Datagrams on Avian Carriers

In reply to Memory Management and Array references by crabbdean

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.