Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Am I Using a Canvas and Images Correctly?
Thanks in advance for helping out a newbie. <smile>

Basically, I have a canvas of a certain size, and I'd like it to
display 120 images in a grid, with the images being refreshed every
second.

So I've created my main window and canvas. I've also a set of photo
objects from  gif files in the following way:

$mw -> Photo('pine', -file => "icons/pine.gif");

To display the photos in the canvas, I'm doing this for each square in
the grid every second (using the repeat method on a button):

$id = $canvas->create( 'image', $gridx, $gridy, '-anchor' => 'nw',
'-image'  => pine );

There is a display sub, called every second by the timer, that uses a
bunch of these $canvas->create statements to paint the various icons
in the proper squares of the grid.

The problem that I am running into, is that the program's display sub
is taking progressively longer to complete each new cycle. The
increase is very slow, but steady.

---

I'm thinking (dunno if I am right) that I must be creating a bunch of
new objects each time the display sub is called, not just merely
painting pixels to a canvas. The steadily mounting numbers of these
objects are increasing the amount of time it takes for Perl/Tk to do
it's thing.

I've tried collecting the IDs of every object created (as above) &
deleting them once the new images are displayed on the canvas like so:

$canvas->delete(objectIDhere);

This does not seem to affect matters.

---

Basically, I'd just like to push some pixels (well, gif pixels) to a
canvas once every second, on a continual basis, without creating a lot
of extra mess in memory. Is there a better way of doing this?


Many Thanks,

JohnTC

Report this thread to moderator Post Follow-up to this message
Old Post
JohnTC
08-09-04 08:55 AM


Re: Am I Using a Canvas and Images Correctly?
On 9 Aug 2004 00:28:11 -0700, john_google@wmuse.com (JohnTC) wrote:

>Basically, I have a canvas of a certain size, and I'd like it to
>display 120 images in a grid, with the images being refreshed every
>second.
>
>So I've created my main window and canvas. I've also a set of photo
>objects from  gif files in the following way:
>
>$mw -> Photo('pine', -file => "icons/pine.gif");
>
>To display the photos in the canvas, I'm doing this for each square in
>the grid every second (using the repeat method on a button):
>
>$id = $canvas->create( 'image', $gridx, $gridy, '-anchor' => 'nw',
>'-image'  => pine );
>
>There is a display sub, called every second by the timer, that uses a
>bunch of these $canvas->create statements to paint the various icons
>in the proper squares of the grid.
>
>The problem that I am running into, is that the program's display sub
>is taking progressively longer to complete each new cycle. The
>increase is very slow, but steady.
>
>---
>
>I'm thinking (dunno if I am right) that I must be creating a bunch of
>new objects each time the display sub is called, not just merely
>painting pixels to a canvas. The steadily mounting numbers of these
>objects are increasing the amount of time it takes for Perl/Tk to do
>it's thing.
>
>I've tried collecting the IDs of every object created (as above) &
>deleting them once the new images are displayed on the canvas like so:
>
>$canvas->delete(objectIDhere);
>
>This does not seem to affect matters.

Yeah, you are running into a common problem with images and Tk, where
you get a memory increase due to stray objects laying around. The first
thing most people try is to destroy the image objects and then recreate
them, it dosn't work.

The general rule is this: Create your photo objects once, and store them
in a hash, and reuse them at every update.

So you would want to create 120 photo objects and store them, like

my %photos
for (1..120){ $photo{$_} = $mw->Photo(-file => "$_ . jpg");
}


Now you can reuse a photo object by blanking it, and reading a new
file into it. So in your update routine something like:

for(1..120){
$photo{$_}->blank;
$photo{$_}->read("$_-new.jpg");
}


I have a simple little memory leak monitor at
http://perlmonks.org?node_id=336856

It will pop up a little box in the upper left corner of your monitor,
and will display the memoery usage of the script, so you can monitor
where these "pseudo-leaks" occur.



--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html

Report this thread to moderator Post Follow-up to this message
Old Post
zentara
08-09-04 08:57 PM


Re: Am I Using a Canvas and Images Correctly?
john_google@wmuse.com (JohnTC) wrote in message news:<647b9871.0408082328.f057da5@posting.g
oogle.com>...
> Thanks in advance for helping out a newbie. <smile>
>
> Basically, I have a canvas of a certain size, and I'd like it to
> display 120 images in a grid, with the images being refreshed every
> second.
>
> So I've created my main window and canvas. I've also a set of photo
> objects from  gif files in the following way:
>
> $mw -> Photo('pine', -file => "icons/pine.gif");
>
> To display the photos in the canvas, I'm doing this for each square in
> the grid every second (using the repeat method on a button):
>
> $id = $canvas->create( 'image', $gridx, $gridy, '-anchor' => 'nw',
> '-image'  => pine );
>
> There is a display sub, called every second by the timer, that uses a
> bunch of these $canvas->create statements to paint the various icons
> in the proper squares of the grid.
>
> The problem that I am running into, is that the program's display sub
> is taking progressively longer to complete each new cycle. The
> increase is very slow, but steady.
>
> ---
>
> I'm thinking (dunno if I am right) that I must be creating a bunch of
> new objects each time the display sub is called, not just merely
> painting pixels to a canvas. The steadily mounting numbers of these
> objects are increasing the amount of time it takes for Perl/Tk to do
> it's thing.
>
> I've tried collecting the IDs of every object created (as above) &
> deleting them once the new images are displayed on the canvas like so:
>
> $canvas->delete(objectIDhere);
>
> This does not seem to affect matters.
>
> ---
>
> Basically, I'd just like to push some pixels (well, gif pixels) to a
> canvas once every second, on a continual basis, without creating a lot
> of extra mess in memory. Is there a better way of doing this?
>

You should also keep track of the created photos and delete them with
the delete() method:

$photo = $canvas->Phote(-file => ...);
..
$photo->delete;

Regards,
Slaven

Report this thread to moderator Post Follow-up to this message
Old Post
Slaven Rezic
08-10-04 08:57 PM


Re: Am I Using a Canvas and Images Correctly?
> > 
>
> You should also keep track of the created photos and delete them with
> the delete() method:
>
>     $photo = $canvas->Phote(-file => ...);
>     ...
>     $photo->delete;
>
> Regards,
>     Slaven

Many thanks, Slaven.

I did need to delete the photo objects, but it turned out that I was
not deleting all of my $canvas->create image objects. Proper
housecleaning works wonders.

JohnTC

Report this thread to moderator Post Follow-up to this message
Old Post
JohnTC
08-12-04 08:58 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PerlTk archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:36 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.