Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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 = "http://192.168.0.25"; 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]); };

Comment on TK Photo Cam - Memory Leak
Download Code
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).
Re: TK Photo Cam - Memory Leak
by johanvdb (Beadle) on Jul 15, 2002 at 22:29 UTC
    Hi,
    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; ... }


    regards,
    johanvdb
      Thanks for the reply and the info on the label !!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://181935]
Approved by Ovid
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-12-22 03:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (110 votes), past polls