For Programmers: Free Programming Magazines  


Home > Archive > PerlTk > June 2007 > Image deletion question?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Image deletion question?
EV

2007-06-14, 7:03 pm

Debian Sarge with TK 800.025
(The problem in this post does the same thing on 804)

I have been learning TK for about a month and so far so good. Although,
coming from Delphi I miss the interactive form building that was very
fast. But anyway...

I have been playing with images and don't quite understand how they are to
be destroyed. The Mastering Perl/TK book is very good, but the references
don't seem to work as stated.

Here I build a reference to a photo object.
$appleimage = $mw->Photo(-file => "apple.gif");

Then I use it.
$topframe->Label(-image=> $appleimage)->place(-x => 5, -y => 5);

Works great.

The book on page 419 says "If we want the image to go away, we must
explicitly delete it. Example - $image->delete;". Sounds good, so when
I am through with the image I use the recommended...

if ($appleimage){
$appleimage->delete;
}

Later, as I began to move images around on the screen, I realised that the
image was not being destroyed. In fact, there were multiple images
stacked one on another.

Running a trace before the delete, I get $appleimage =
TK::Photo=HASH(0878etc); After the delete I get the same thing so nothing
got destroyed.

Apparently I am not the only newbie having troubles since I can see the
end of a couple of references to about the same thing. Unfortunately, my
news server is fairly shallow and doesn't go back very far.

What is the book saying that I am not seeing correctly?

Thanks anyone
EV
QoS@domain.invalid

2007-06-15, 4:11 am


EV <ev@nowhere.com> wrote in message-id: <pan.2007.06.14.16.15.50.117292@nowhere.com>

>
>Debian Sarge with TK 800.025
>(The problem in this post does the same thing on 804)
>
>I have been learning TK for about a month and so far so good. Although,
>coming from Delphi I miss the interactive form building that was very
>fast. But anyway...
>
>I have been playing with images and don't quite understand how they are to
>be destroyed. The Mastering Perl/TK book is very good, but the references
>don't seem to work as stated.
>
>Here I build a reference to a photo object.
> $appleimage = $mw->Photo(-file => "apple.gif");
>
>Then I use it.
> $topframe->Label(-image=> $appleimage)->place(-x => 5, -y => 5);
>
>Works great.
>
>The book on page 419 says "If we want the image to go away, we must
>explicitly delete it. Example - $image->delete;". Sounds good, so when
>I am through with the image I use the recommended...
>
> if ($appleimage){
> $appleimage->delete;
> }
>
>Later, as I began to move images around on the screen, I realised that the
>image was not being destroyed. In fact, there were multiple images
>stacked one on another.
>
>Running a trace before the delete, I get $appleimage TK::Photo=HASH(0878etc); After the delete I get the same thing so nothing
>got destroyed.
>
>Apparently I am not the only newbie having troubles since I can see the
>end of a couple of references to about the same thing. Unfortunately, my
>news server is fairly shallow and doesn't go back very far.
>
>What is the book saying that I am not seeing correctly?
>
>Thanks anyone
>EV


Whilst it is best to hide the label or change the image being displayed
by the label, you could try to remove the image from the screen by destroying
the label itself.

The hash in your trace is probably empty.


zentara

2007-06-15, 8:03 am

On Thu, 14 Jun 2007 11:15:50 -0500, EV <ev@nowhere.com> wrote:

>Debian Sarge with TK 800.025
>(The problem in this post does the same thing on 804)


>Here I build a reference to a photo object.
> $appleimage = $mw->Photo(-file => "apple.gif");
>
>Then I use it.
> $topframe->Label(-image=> $appleimage)->place(-x => 5, -y => 5);
>
>Works great.
>
>The book on page 419 says "If we want the image to go away, we must
>explicitly delete it. Example - $image->delete;". Sounds good, so when
>I am through with the image I use the recommended...
>
> if ($appleimage){
> $appleimage->delete;
> }
>
>Later, as I began to move images around on the screen, I realised that the
>image was not being destroyed. In fact, there were multiple images
>stacked one on another.


>What is the book saying that I am not seeing correctly?
>
>Thanks anyone
>EV


Tk Photo objects are probably the number 1 source of unwanted memory
gains in scripts, so first watch your memory as you try updating labels.

Don't try to destroy a Photo object then create a new one.
What you want to do is reuse Photo objects, then update the label.

Unless you actually show a simple script that demonstrates the problem,
we can't be more specific.

Here is the basic idea on Photo object reuse.

#!/usr/bin/perl -w
use strict;
use Tk;
use Tk::JPEG;

my $c;

my $dir = ".";
my @fl = <*.jpg>;

print "@fl\n";

my $main = new MainWindow;

#make just 2 photo objects and reuse them, so your
#memory won't increase with every image.
# and load photo into a widget, not the main window
# let the label set the size of the window
# you could do it in the main if you want, it just
# isn't usually done like that, because you usually
# pack other things into the main window.

my $photo_obj = $main->Photo(-file => '' );
my $photo_obj_scaled = $main->Photo(-file => '' );

my $display = $main->Label(-width => 200,
-height => 200,
-image => $photo_obj_scaled,
)->pack(-fill=>'both', -expand=>1);

nextp();

my $nxt = $main->Button('-text' => "Next", '-command' =>
\&nextp)->pack();

$main->Button(-text => 'exit',
-command => sub{destroy $main}
)->pack(-anchor => 'e');
MainLoop;


sub loadpic {
my $filename = shift;
my $size;
my $orientation;
$photo_obj->blank; #clear out old jpg
$photo_obj_scaled->blank;
my ($h,$w) = ($main->height, $main->width);
#
# how to scale the pic?

#Tk can only zoom or shrink by a factor, so
#you need to determine the size of the image
#then decide whether it needs shrinking or zooming.
#but Tk itself won't get the size of the image without
#external modules like Image::Info

#I just show a shrink here, for demoing the idea.

$photo_obj->read($filename);
$photo_obj_scaled->copy($photo_obj, -subsample => 4,4 );

$display->configure(-image => $photo_obj_scaled);

return;
}

sub nextp {

push (@fl,shift(@fl)); #circular list

my $f = $fl[0];
loadpic("$dir/$f");
return;
}
__END__


zentara




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

2007-06-15, 8:03 am

> Whilst it is best to hide the label or change the image being displayed
> by the label, you could try to remove the image from the screen by destroying
> the label itself.
>
> The hash in your trace is probably empty.


I will give it a try tonight, but the book specifically says that the
image object lives on even if the calling object, in this case a label, is
killed.

Thanks
Ken
EV

2007-06-15, 8:03 am


> Unless you actually show a simple script that demonstrates the problem,
> we can't be more specific.
>
> Here is the basic idea on Photo object reuse.
>



Thanks a bunch. I will give your example a try tonight or this wend.

EV

EV

2007-06-18, 8:03 am



> Tk Photo objects are probably the number 1 source of unwanted memory
> gains in scripts, so first watch your memory as you try updating labels.
>

Man, you are right. Tk is a solid product overall, but in this area there
are some massive holes. After playing with the photo object all wend,
I learned a ton about Tk, but also that the book and the documentation
don't match the photo object code.


> Don't try to destroy a Photo object then create a new one. What you want
> to do is reuse Photo objects, then update the label.
>
>

As near as I can tell, with version 800 (which is what I have at home
since 804 won't build on my Sarge), a photo object can't be destroyed
PERIOD. No matter what method I tried to kill it with, it was always there
to be used.

So I took your advice and just built a couple and reused them.

EV

Steve Lidie

2007-06-18, 10:04 pm

EV <ev@nowhere.com> wrote:
>
>
> Man, you are right. Tk is a solid product overall, but in this area there
> are some massive holes. After playing with the photo object all wend,
> I learned a ton about Tk, but also that the book and the documentation
> don't match the photo object code.
>
>
> As near as I can tell, with version 800 (which is what I have at home
> since 804 won't build on my Sarge), a photo object can't be destroyed
> PERIOD. No matter what method I tried to kill it with, it was always there
> to be used.
>
> So I took your advice and just built a couple and reused them.
>
> EV
>


I can't verify that a Photo's memory is reclaimed when it's deleted,
but this example shows that $image->delete() does indeed delete a
Photo :)


#!/usr/local/bin/perl -w
use Tk;
use strict;

my $mw = MainWindow->new;
my $p = $mw->Photo(-file => Tk->findINC('Xcamel.gif'));

my $l = $mw->Label( -image => $p ) ->pack;
$mw->update;
$mw->after(2000);

$p->delete;
$mw->update;
$mw->after(2000);

my $l2 = $mw->Label( -image => $p ) ->pack;

MainLoop;



[lusol@dragonfly:~/Desktop] ./rotate
image "image1" doesn't exist at /Users/lusol/local/lib/perl5/site_perl/5.8.6/darwin-2level/Tk/Widget.pm line 205.
at ./rotate line 16
[lusol@dragonfly:~/Desktop]


That being said, I'm all for reusing Tk widgets and objects.

Steve

--
Posted via a free Usenet account from http://www.teranews.com

EV

2007-06-19, 8:02 am


>
> I can't verify that a Photo's memory is reclaimed when it's deleted,
> but this example shows that $image->delete() does indeed delete a
> Photo :)
>


I will give that a try tonight when I get home. Like you said, I fixed
the problem by reusing the same object, but I still want to see the
diffence between the book's deletion and yours.

Which version of TK are you using?

Thanks
EV
Steve Lidie

2007-06-21, 8:03 am

EV <ev@nowhere.com> wrote:
>
>
> I will give that a try tonight when I get home. Like you said, I fixed
> the problem by reusing the same object, but I still want to see the
> diffence between the book's deletion and yours.
>
> Which version of TK are you using?


804.027, gotta' stay as current as possible :)

>
> Thanks
> EV


--
Posted via a free Usenet account from http://www.teranews.com

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com