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

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.

Replies are listed 'Best First'.
Re: Writing a REAL destructor
by Abigail-II (Bishop) on Jun 05, 2002 at 14:58 UTC
    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.

        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.

        What do you mean? First you say that if the ref count goes to 0, the object may not be swept right away (which isn't true - this is perl5, not perl6 nor Java), but if you undef the last reference, the ref count goes to 0 and it gets destroyed.

        That doesn't make any sense. Either the object is collected if the ref count goes to 0, or it isn't. But how the ref count goes to 0 doesn't matter.

        But in perl5, the object is collected right after the ref count goes to 0.

        Abigail