in reply to Acky Memory
How do you delete an unwanted object?
The short answer: use $o->DESTROY; instead of undef $0.
The long answer involves the fact that Perl's garbage collection mechanism can be fooled. It is based on reference counting, and fails to reclaim memory as soon as it involves circular references. So you might undef your $o variable, but the underlying structure might still have a non-null reference count and thus not be freed. In order to deal with those "sticky" objects, modules can (and should) provide a DESTROY method that can be called either explicitely by the program or implicitely when an object goes out of scope.
Which by the way also answers your other question about whether you would need to do something like this: yes!
Now for the task at hand: Gtk actually provides a DESTROY method for Gtk::Window objects so you could try using it instead of undef. The weird thing though is that in your example the $o object goes out of scope at the end of each loop, so it should be reclaimed by the Garbage Collector. And as I am not familiar with the internals (nor the externals!) of Gtk I have no idea whether there is a memory leak in the module or whether this is normal.
Just to make you feel even worse here is a piece of code from the test.pl file that comes with Gtk and which looks conspicuously similar to what you wrote, they don't use DESTROY but just undef:
sub destroy_window { my($widget, $windowref, $w2) = @_; $$windowref = undef; $w2 = undef if defined $w2; 0; }
Go figure...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Acky Memory
by Dominus (Parson) on Nov 12, 2000 at 18:53 UTC | |
by mirod (Canon) on Nov 12, 2000 at 19:35 UTC |