in reply to (MeowChow) Re: Writing a REAL destructor
in thread Writing a REAL destructor

A purely theoretical follow up question (just because I am curious):

When you undef an object, is it really freed immideately, or just marked up for collecting, while the GC decides when to really take care of it all by itself? Reason I ask is that I seem to recall that you can not trust this to happen at any given point no matter what you do - but the odds are better if you undef.

Note that if you need that finegrained control over your memory and/or objects, if this really matters, then you probably have lots of other, more important problems, such as choice of language, hardware or the problem/project itself. :) So I am just asking technically, what really happens?


You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue.

Replies are listed 'Best First'.
Re^3: Writing a REAL destructor
by Aristotle (Chancellor) on Jun 05, 2002 at 09:07 UTC
    I believe undefing a reference was given extra magic to act as a forceful and immediate call to action for the garbage collector. I may be wrong and objects may always be immediately collected as soon as the ref counter hits bottom though.

    Makeshifts last the longest.

      You are indeed wrong. undef is not a garantee an object will be DESTROYed. It will only be destroyed if the ref counter goes to 0.

      Image some code like this:

      { my $obj = Harf -> new; my $obj2 = $obj; undef $obj; # Does *not* call DESTROY! .... } # Only here DESTROY is called, as also $obj2 goes out of scope.

      Abigail

        That is not what I meant, however; I didn't expect I came across as saying objects with a ref counter != 0 can be garbage collected. What I was saying is that as far as I know, when the ref count goes to 0, the object may not be swept right away; while if you bring the ref count down to 0 explicitly with an undef on the last reference, then it will be collected immediately.

        Makeshifts last the longest.

(MeowChow) Re3: Writing a REAL destructor
by MeowChow (Vicar) on Jun 05, 2002 at 08:47 UTC
    A purely theoretical follow up answer (because I know very little about perl internals):

    I believe it's freed immediately. The issue you're probably recalling is that there's no orderly destruction of objects when a program is terminating.

       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print