perldough has asked for the wisdom of the Perl Monks concerning the following question:
I have written a program which leaks approximately 60MBs every time a string of routines runs (starts with &start_problems).
With a debugger and the task manager, I'm pretty sure I have narrowed the source of the leak to $img and $zimg (which contain an image and a zoomed version of the image, respectively), but I'm not having any success eliminating these leaks. Perhaps someone could offer some insight?
How to use the test program:Thanks,use Tk; use Tk; use Tk::PNG; use Tk::LabFrame; use Tk::Radiobutton; use Tk::Pane; use Tk::WorldCanvas; use strict; our $currdwg = "./largeimage.png"; our $ZC; our $sr; our ($imgX, $imgY); our $MW = MainWindow->new(-title => " PROXY FOR MW"); $MW->geometry("+4+5"); review($MW); MainLoop; sub review { my ($MW) = @_; my ($RW, $CW) = makeReviewScreen($MW); } sub makeReviewScreen { my ($MW) = @_; our $width = $MW->screenwidth - 145; my $RW = $MW->Toplevel(-title => 'XXX'); $RW->geometry("+0+5"); my $LW = $RW->Frame; my $CW = $RW->Scrolled('WorldCanvas', -width => $width, -height => '480', -background => 'white', -borderwidth => '3', -scrollbars => 'se', -relief => 'sunken', -scrollregion => [-100,0, 6000,40 +00]); $ZC = new ZoomCanvas($CW, 1); $LW->pack(-side => 'left', -fill => 'y', -expand => '1', -ancho +r => 'nw', ); $CW->pack(-side => 'left', -fill => 'both', -expand => '1' ); $RW->bind('<Up>' => sub { print "start\n"; start_problems($MW, $ZC); print "end\n"; } ); print "init start\n"; start_problems($MW, $ZC); print "init end\n"; return ( $RW, $CW); } sub start_problems { my ($MW, $ZC) = @_; my @items = $ZC->find('all'); for my $item (@items) { $ZC->delete($item); } show_drawing($MW, $ZC); } sub show_drawing { use GD::Image; use MIME::Base64 qw[ encode_base64 ]; my ($MW, $ZC) = @_; #----------------- Display drawing at specified +- zoom --------- +---------# my $orig = GD::Image->new($currdwg); my ($xMax, $yMax) = $orig->getBounds; $sr = 3500/ $xMax; ($imgX, $imgY) = map { int($_ * $sr) } $orig->getBounds; my $small = GD::Image->new($imgX, $imgY); $small->copyResampled($orig, 0, 0, 0, 0, $imgX, $imgY, $orig->getB +ounds); my $img = $MW->Photo(-data => encode_base64( $small->png ), -format => 'png' ); $MW->configure( -title => " REVIEW INTERLOCKING DRAWING - $currd +wg ($xMax x $yMax)"); my $dwg = $ZC->createImage($MW,0,0, -anchor => 'nw', -image => $im +g, -tags => 'dwg'); $ZC->centerTags($dwg); undef $img; undef $small; undef $orig; return ([$xMax, $yMax],[$imgX, $imgY]); } package ZoomCanvas; use strict; use Tk; use feature 'state'; sub new { my $class = shift; my $canvas = shift; my $zoom = shift; # Quick loose check to ensure $canvas is a WorldCanvas my $test1 = (ref($canvas) eq "Tk::Frame"); use Data::Dumper; my $test2 = (Dumper($canvas) =~ "worldcanvas"); if (! ($test1 && $test2)) { die "ZoomCanvas::new: canvas argument not WorldCanvas!\n" } if (! defined $zoom) { $zoom =1; } my $self = { CANVAS => $canvas, ZOOM => $zoom }; bless $self, $class; return $self; } sub AUTOLOAD { my @args = @_; my $self = shift @args; my $substring = $ZoomCanvas::AUTOLOAD; my @splitSub = split("::", $substring); my $sub = $splitSub[1]; my $canvas = $self->{CANVAS}; return $canvas->$sub(@args); } sub createImage { use Tk::Photo; use Storable; use Storable qw(nstore dclone); # Create state vars state $xoff; state $yoff; state %miscArgs; state $MW; my @args = @_; my $self = shift @args; my $canvas = $self->{CANVAS}; my $MW_tmp = shift @args; # Original args my $xoff_tmp = shift @args; my $yoff_tmp = shift @args; my %miscArgs_tmp = @args; if ( defined $MW_tmp && defined $xoff_tmp && defined $yoff_tmp && (keys %miscArgs_tmp) ) { $MW = $MW_tmp; $xoff = $xoff_tmp; $yoff = $yoff_tmp; if (exists $miscArgs{-image}) { delete $miscArgs{-image}; } %miscArgs = %miscArgs_tmp; } my $img = $miscArgs{-image}; my $zimg = $MW->Photo(); ($self->{ZOOM} > 1) ? $zimg->copy($img, -zoom => $self->{ZOOM +}) : $zimg->copy($img, -subsample => (1 / $self-> +{ZOOM})); $miscArgs{-image} = $zimg; my @miscArgs_list = %miscArgs; my $return = $canvas->createImage($xoff,$yoff, @miscArgs_list); $miscArgs{-image} = $img; delete $miscArgs{-image}; delete $miscArgs_tmp{-image}; undef $img; undef $zimg; return $return; } 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Hunting for a memory leak
by BrowserUk (Patriarch) on Aug 07, 2012 at 16:20 UTC | |
by perldough (Sexton) on Aug 07, 2012 at 17:18 UTC | |
by BrowserUk (Patriarch) on Aug 07, 2012 at 17:30 UTC | |
by perldough (Sexton) on Aug 08, 2012 at 14:10 UTC | |
by BrowserUk (Patriarch) on Aug 08, 2012 at 14:20 UTC | |
by BrowserUk (Patriarch) on Aug 07, 2012 at 18:30 UTC | |
by Anonymous Monk on Aug 08, 2012 at 03:13 UTC | |
by perldough (Sexton) on Aug 08, 2012 at 14:19 UTC | |
by Anonymous Monk on Aug 10, 2012 at 07:44 UTC | |
|
Re: Hunting for a memory leak
by zentara (Cardinal) on Aug 07, 2012 at 18:34 UTC | |
by perldough (Sexton) on Aug 08, 2012 at 14:32 UTC | |
by zentara (Cardinal) on Aug 08, 2012 at 16:02 UTC | |
|
Re: Hunting for a memory leak
by locked_user sundialsvc4 (Abbot) on Aug 08, 2012 at 03:06 UTC | |
by BrowserUk (Patriarch) on Aug 08, 2012 at 08:26 UTC | |
by perldough (Sexton) on Aug 08, 2012 at 14:36 UTC | |
by snakebites (Initiate) on Aug 08, 2012 at 05:53 UTC |