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(); }

In reply to Tk Image Flipping - Out of Memory by ichimunki

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.