nik@tiuk.ti.com (Nick Ing-Simmons)
 Re: b11.02 mem leak
 Re: b11.02 mem leak
 16 May 96 13:28:56 GMT
 via, but not speaking for : Texas Instruments Ltd.
-  Newsgroups:
-  comp.lang.perl.tk
-  References:
-  <199605161300.JAA18685@Turkey.CC.Lehigh.EDU>
Stephen O Lidie  writes:
>Nick, Felix found a leak that indeed will crash your machine very rapidly (-;
>Felix, Nick normally reads only the pTk list, that's why I'm forwarding your
>problem.
Like C code: 
int main()
{
 int i;
 for (i-0; i < 50000; i++)
  {
   char *p = malloc(1024);
  }
}
That leaks as there is no free().
>
>
># WARNING: LEAKS MEMORY LIKE A SIEVE!  DON'T LET IT RUN AWAY!
>use Tk;
>use Tk::Pixmap;
>$fish = MainWindow->new();
>for ($i = 0; $i < 50000; $i++) {
>    $blah = $fish->Pixmap(-file => 'thing.xpm');
>}
That leaks for same reason  - no ->delete
This also "leaks"
use Tk;
$fish = MainWindow->new();
for ($i = 0; $i < 10; $i++) 
{
 $blah = $fish->Button(-text => 'button$i'');
 $blah->pack;
}
No ->destroy.
But, you would not want all buttons deleted from screen
as soon as $blah went out of scope.
Likewise the "images" you have created above are still accessible via 
(auto-generated) names: 
#!/usr/local/bin/perl -w
use Tk;
use Tk::Pixmap;
$fish = MainWindow->new();
for ($i = 0; $i < 10; $i++) {
    $blah = $fish->Bitmap(-file => Tk->findINC('Tk.xbm'));
}
foreach $name ($fish->imageNames)
 {
  print "Have $name\n";
 }
__END__
All Tk "objects" have to be destroyed (deleted) explicitly.
There are non-user references to the objects from the MainWindow
which needs to know its sub-objects (so it can delete them when it is
destroyed...) - so you need to use ->delete
to break these references before perl structures will get disposed.
>
>I would expect this program not to grow in memory, but it apparently
>doesn't properly destruct the Pixmaps as they lose their 'last reference'
>(as $blah gets assigned to something new), so it grows and grows.
>
>I'm writing an application which aims to stay running forever and
>might get new pixmaps to display every so often.  However, I can
>find no way to destroy old Pixmap objects and reclaim their memory,
>which would obviously be bad.
>
>Any thoughts?
To destroy image you need to : 
1. Make sure no widget has it in use as a -image ...
2. $image->delete;
3. Get perl refcount to 0 - i.e. variables out of scope
   deleted from hashes/arrays etc.
==========================================================================
This message was posted through the Stanford campus mailing list
server.  If you wish to unsubscribe from this mailing list, send the
message body of "unsubscribe ptk" to majordomo@lists.stanford.edu
-- 
This article was gatewayed from the ptk@lists.stanford.edu mailing list.
Problems? refay@carbon.cudenver.edu. Subscriptions: Majordomo@lists.Stanford.EDU