in reply to Re: Acky Memory
in thread Acky Memory

This is really bad advice. Calling the DESTROY method on an object will not clean up its memory and can cause all sorts of weird problems because you have invoked the destructor for an object that has not actually been destroyed. You should never invoke the DESTROY method explicitly.

When Perl sees that an object is no longer being used by anyone, it will reclaim the memory for the object automatically. Just before it does this, it will call the DESTROY method automatically, to perform any final cleanup that the object requires, such as closing any filehandles that it might contain.

If you call DESTROY yourself, you will have an object in an inconsistent state, because DESTROY thought that the object was about to be destroyed. But it was not destroyed, because some part of the program still has a reference to it; you called DESTROY permaturely. And then later, when Perl sees that the object is no longer useful, it will reclaim the memory for the object and call DESTROY a second time. This could easily cause all sorts of havoc because a DESTROY method is only supposed to be called once.

Finally, you run the risk of a fatal run time error because not every object has a DESTROY method, and if you call the DESTROY method on an object that doesn't have one, your program will die.

Summary: never call $o->DESTROY.

Replies are listed 'Best First'.
RE: Re: Acky Memory
by mirod (Canon) on Nov 12, 2000 at 19:35 UTC

    OK, I stand corrected.

    I was thinking of a case where the automatic GC doesn't kick in for example because the object never goes out of scope, but actually just doing undef $o should be enough, my apologies, and on to the Worst Nodes list!