in reply to DESTROYing an object

We are looking at the wrong sequence of events. Calling DESTROY not subsequently involes garbage collector, on the contrary, it is the garbage collector will call the object's DESTROY method, if there is one defined, before it collect the 'garbage'.

(A small quiz for everyone. Does the following two pieces of code cause memory leak? If not sure, test them with some system resourse monitor.)

first one: while (1) { my $variable = \$variable; } second piece: define a class with this new: sub new { my $self = {}; $self->{"parent"} = $self; $self->{"child"} = $self; bless $self; return $self; }

Replies are listed 'Best First'.
Re: DESTROYing an object
by Abigail-II (Bishop) on Dec 12, 2002 at 12:20 UTC
    The first piece of code doesn't. In each iteration, a reference to an outer $variable is taken, and stored in the innter $variable. But the space for this variable is reused in the next iteration - it went out of scope at the end of the previous iteration.

    As for the second one, it depends. What else is there in the code? Each object created my this constructor will have two references to itself, so unless there's code to remove the self referentials, the objects will not be garbage collected before the end of the program. But that in itself isn't necessarely a memory leak.

    Abigail