Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

TK Photo Cam - Memory Leak

by Anonymous Monk
on Jul 15, 2002 at 21:52 UTC ( #181935=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dearest Monks,
Can anyone help me with this simple little app I wrote to grab and display web cam images off of a local server. The problem is that it keeps eating up memory with each update. I know im missing something simple to release the memory but I cannot seem to find it. Please help ! On a side note does anyone know how I can make the image scale in size along with the form ?
Thank you very much !!
#!/usr/bin/perl -w use strict; use Tk; use Tk::JPEG; use LWP::Simple; use MIME::Base64; my $UPDATE_INTERVAL = 10; my $CAM_URL = ""; my $CAM_TITLE = "Cam - North View"; my $mw = MainWindow->new; $mw->title($CAM_TITLE); my $buttonImage = $mw->Photo( -format => 'jpeg' ); my $TT = $mw->Button(-image => $buttonImage)->pack; $mw->after( $UPDATE_INTERVAL,[\&update_image]); MainLoop; sub update_image { my $data = encode_base64( get($CAM_URL) ); $buttonImage = $mw->Photo(-data => $data, -format => 'jpeg'); $TT->configure(-image => $buttonImage); $mw->after( $UPDATE_INTERVAL,[\&update_image]); };

Replies are listed 'Best First'.
Re: TK Photo Cam - Memory Leak
by johanvdb (Beadle) on Jul 15, 2002 at 22:29 UTC
    You seem to be creating new Photo objects with every iteration ...
    Instead of recreating the $buttonImage = $mw->Photo(-data => $data, -format => 'jpeg');,
    you can opt for using the configure method on it ...
    You also do not need to use the Button, a Label suffices.

    ... use Tk; use Tk::JPEG; ... my $mw = MainWindow->new(); my $image = $mw->Photo(); $mw->Label( -image=>$image )->pack; ... sub update_image { ... $image->configure( -format => 'jpeg', -data => $data ); $mw->update; ... }

      Thanks for the reply and the info on the label !!
Re: TK Photo Cam - Memory Leak
by Anonymous Monk on Jul 15, 2002 at 22:26 UTC
    Owell, I found the answer to my question. I guess I should'nt have rushed to post the question. To anyone else interested the following fixes the problem. Im still looking for an answer (or some hints) on the second part of my initial question.
    sub update_image { my $data = encode_base64( get($CAM_URL) ); $buttonImage->configure(-data => $data, -format => 'jpeg'); $TT->configure(-image => $buttonImage); $mw->after( $UPDATE_INTERVAL,[\&update_image]); };
      You might try the Tk::Photo attribute '-subsample', to get the image to reduce by literally removing every x pixel from the image. You might also use ImageMagick (which has perl modules available and works on both Windows and Linux/Unix) to resize the image (I'm not sure if there is a way to use ImageMagick on data in memory, I've only ever used it on files).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://181935]
Approved by Ovid
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2018-06-24 05:53 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.