ichimunki has asked for the wisdom of the Perl Monks concerning the following question:

I'm using a Tk::Text widget to hold and flip through images. If the image is too big to fit on the screen I do a quick resize (hence the two Photo objects).

The program works fine, except that it eventually crashes with "out of memory" issues.

I've looked for similar questions and documentation on this where I can, but don't see how, after I've destroyed the Photo widgets and deleted the image from the Text widget these are still building up in memory.

I used to display the images on a button and had no issues with destroy and memory leaks there, so I suspect imageCreate() is at issue. So what do I do to fix this? Change my approach, or some other trick to free that memory up? Thanks.
sub show_next_image { my( $id, $filename ) = get_next_pic(); my $raw_image = $Image_Window->Photo( '-format' => 'jpeg', -file => $filename ); my ($img_w, $img_h) = ($raw_image->width, $raw_image->height); my $max_width = $Main->screenwidth()-20; my $max_height = $Main->screenheight()-75; my $xfactor = $img_w / $max_width; my $yfactor = $img_h / $max_height; my $intfactor = $xfactor > $yfactor ? int($xfactor) : int($yfactor +); $intfactor += 1; my $image = $Image_Window->Photo(); $image->copy( $raw_image, -subsample => $intfactor); my $reduced_flag = ( $raw_image->width == $image->width ) ? 'full-size' : "reduced by $intfactor"; $Image_Window->delete( '0.1', 'end' ); $Image_Window->insert( '0.1', "$reduced_flag\n" ); my $image_widget = $Image_Window->imageCreate( 'end', -image => $i +mage ); $raw_image->destroy(); $image->destroy(); }

Replies are listed 'Best First'.
Re: Tk Image Flipping - Out of Memory
by Yohimbe (Pilgrim) on Feb 08, 2001 at 09:10 UTC
    I've been playing with perl/Tk, and had several problems with images.
    I'm thinking that its possible that you have found a bug in the current version. You are up to date with the latest and greatest in CPAN? Have you a bit of driver code that benchmarks your memory usage yet? As in, load 1, measure memory, destroy, load next, etc?
    Also, I'd be interested to see what happened if you don't scale your images.
    HTH
    --
    Jay "Yohimbe" Thorne, alpha geek for UserFriendly
Re: Tk Image Flipping - Out of Memory
by kschwab (Vicar) on Feb 08, 2001 at 09:19 UTC
    I've had trouble in general with objects not going out of scope in Tk. Create a DESTROY method and see if it ever goes out of scope.

    I'm guessing Tk keeps refs to widgets around somewhere internally ?

      I won't characterize this as (much) more than a wild guess, but I'll throw it out here anyway because sometimes that just motivates someone to correct me.

      Tk uses a lot of XS code and a lot of people that write XS code don't understand reference counting (especially when using "mortal" variables). If you make your reference counts too small, then things get free()d too soon and you notice. If you make your reference counts too big, then stuff hangs around forever and you don't notice unless you "go out of your way" to test for memory leaks (which you should do when writing XS code that creates Perl data items).

      So, anyway, back to the wild allegations. I suspect that Tk's XS code often sets reference counts too high which causes things to never be destroyed. I've certainly put in DESTROY methods and never gotten them to fire once Tk has a reference to that object.

              - tye (but my friends call me "Tye")