jettero has asked for the wisdom of the Perl Monks concerning the following question:

How do you delete an unwanted object? Is there a generic way to do it?

Not that you'd ever need to do something like this, but could you?

use Gtk; Gtk->init; Gtk->set_locale; for(1..200) { my $o = new Gtk::Window; undef $o; sleep 1; print "$_\n"; }
Load up top, hit 'M', and watch the thing grow evilly... I tried using delete instead of undef, but it didn't help.

I actually have a very good reason for this. I need to generate Gtk::CList's on the fly, but I realized I have no way to delete the old ones... I could really use some help here. Actually, a solution to changing the number of columns in a Gtk::CList would be equally helpful. ;)

How do you delete an unwanted object in perl?

Replies are listed 'Best First'.
Re: How do you delete an unwanted object?
by m_turner (Sexton) on Nov 13, 2000 at 04:33 UTC
    undef does indeed invoke garbage destruction as can be shown in the following code.
    #!/usr/bin/perl -w use strict; my $foo = new stuff; my $bar = new stuff; print $foo,"\n"; $foo = undef; print STDERR "foo gone\n"; exit; package stuff; sub new { my $self = []; bless $self; return $self; } sub DESTROY { my ($self) = @_; print STDERR "destroying...\n"; } 1;
    The output is:
    stuff=ARRAY(0x80cb9c8)
    destroying...
    foo gone
    destroying...
    
    As you can see:
    1. $foo gets undef'ed
    2. DESTROY gets called for $foo
    3. program exits, begins garbage collection
    4. DESTROY gets called for $bar
    5. program exits
    The real question is do Gtk::CList objects get refrenced anywhere else or have circluar data structures that will prevent them from getting destroyed with the undef?