in reply to Tk, HLists and memory leakage
What I do is place Data::Dumper statements at critical points in the program, and watch for the variables growing....like extra hash keys.
If I really need to rethink my wanton creation and destruction of widgets, can anyone suggest some strategies for keeping a pool of widgets around and configuring them as needed?
I think you answered your own question. Hlist entries can be reconfigured, so don't delete and create new ones.You usually do this, by separating your actual data storage, from the displaying of the data. In other words, don't use the widget to store your data, use the widget to display data stored in another hash. That way the widgets are just a "window to the screen" to display what is in some underlying data hash. They remain the same, just being reconfigured to display the new data.
Here are a few examples.
#!/usr/bin/perl use strict; use Tk; use Tk::HList; my $mw = MainWindow->new(); my $label = $mw->Label( -width => 15 ); my $hlist = $mw->Scrolled('HList', -itemtype => 'text', -selectmode => 'single', )->pack; foreach (1..1000) { my $text = $_.time; $hlist->add( $_, -text => $text ); } my $button = $mw->Button(-text => 'Leak Test', -command => \&testleak)->pack; MainLoop; sub testleak{ #foreach (1..1000) { # # $hlist->delete('all'); # $hlist->delete('entry',$_); # } foreach (1..1000) { my $text = $_.time; # $hlist->add( $_, -text => $text ); $hlist->entryconfigure( $_, -text => $text ); } }
#!/usr/bin/perl use strict; use Tk; use Tk::HList; my $mw = MainWindow->new(); #create some sample data my %data; foreach (0..100) { $data{$_}{'name'} = 'name'.$_; $data{$_}{'id'} = rand(time); $data{$_}{'priority'} = int rand 50; } #get random list of keys my @keys = keys %data; ################# my $h = $mw->Scrolled( 'HList', -header => 1, -columns => 3, -width => 40, -height => 40, -takefocus => 1, -background => 'steelblue', -foreground =>'snow', -selectmode => 'single', -selectforeground => 'pink', -selectbackground => 'black', # -browsecmd => \&browseThis, )->pack(-side => "left", -anchor => "n"); $h->header('create', 0, -text => ' Name ', -borderwidth => 3, -headerbackground => 'steelblue', -relief => 'raised'); $h->header('create', 1, -text => ' ID ', -borderwidth => 3, -headerbackground => 'lightsteelblue', -relief => 'raised'); $h->header('create', 2, -text => ' Priority ', -borderwidth => 3, -headerbackground => 'lightgreen', -relief => 'raised'); foreach (@keys) { my $e = $h->addchild(""); #will add at end $h->itemCreate ($e, 0, -itemtype => 'text', -text => $data{$_}{'name'}, ); $h->itemCreate($e, 1, -itemtype => 'text', -text => $data{$_}{'id'}, ); $h->itemCreate($e, 2, -itemtype => 'text', -text => $data{$_}{'priority'}, ); } my $button = $mw->Button(-text => 'exit', -command => sub{exit})->pack; my $sortid = $mw->Button(-text => 'Sort by Id', -command => [\&sort_me,1] )->pack; my $sortpriority = $mw->Button(-text => 'Sort by Priority', -command => [\&sort_me,2] )->pack; MainLoop; ######################################################### sub sort_me{ my $col = shift; my @entries = $h->info('children'); my @to_be_sorted =(); foreach my $entry(@entries){ push @to_be_sorted, [ $h->itemCget($entry,0,'text'), $h->itemCget($entry,1,'text'), $h->itemCget($entry,2,'text'), ]; } my @sorted = sort{ $a->[$col] cmp $b->[$col] || #primary sort ascii + $a->[1] <=> $b->[1] #secondary sort num +eric } @to_be_sorted; my $entry = 0; foreach my $aref (@sorted){ # print $aref->[0],' ',$aref->[1],' ',$aref->[1],"\n"; $h->itemConfigure( $entry, 0, 'text' => $aref->[0] ); $h->itemConfigure( $entry, 1, 'text' => $aref->[1] ); $h->itemConfigure( $entry, 2, 'text' => $aref->[2] ); $entry++; } $mw->update; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Tk, HLists and memory leakage
by TGI (Parson) on Oct 20, 2005 at 18:50 UTC | |
by zentara (Cardinal) on Oct 21, 2005 at 10:09 UTC | |
by TGI (Parson) on Oct 21, 2005 at 19:13 UTC | |
by zentara (Cardinal) on Oct 22, 2005 at 10:51 UTC |