realbasic-nug
[Top] [All Lists]

Re: Dangerous deallocation/circular reference...

To: REALbasic Network Users Group <realbasic-nug at lists dot realsoftware dot com>
Subject: Re: Dangerous deallocation/circular reference...
From: Brady Duga <duga at ljug dot com>
Date: Thu, 11 Jul 2002 12:03:12 -0700

On Thursday, July 11, 2002, at 11:42  AM, Joseph J. Strout wrote:

At 11:01 AM -0700 7/11/02, Brady Duga wrote:
I am worried because the reference count of the Tickler becomes 0 from a call the Tickler is making.

No, it doesn't. You said that you nil the reference TO the Tickler. The Tickled object still has a reference to it -- it must, since it invoked the Tickle method somehow.

Right - I nil the reference to the Tickler - the reference that the Tickled has to it. So the Tickled object no longer has a reference to the tickler (the Tickle method was invoked by the Tickler, not the Tickled).


And in fact, as you've described it, the Tickled object won't go away at all. The Tickler still has a reference to it, so until the Tickler object dies (which presumably won't be until the app exits), anything it has a reference to will stay alive too.

I assume the Tickler would go away when its ref count dropped to 0.


So, what you need to do is tell the Tickler to release its reference to the object that no longer needs tickles. Then, the object will be destroyed. And yes, this is safe.

I'm not sure I explained this correctly. Let me try again - in some object, we have this code:

sub StartMeUp()
        dim lv as SomeClassWhichImplementsTickled

        lv = new SomeClassWhichImplementsTickled
        lv.DoSomethingforTenSeconds()
        return
end sub

In SomeClassWhichImplementsTickled:

sub SomeClassWhichImplementsTickled()
        mTickler = new Tickler(me)
end sub

sub Tickle()
        If IAmDone() then       
                mTickler = nil
        else
                KeepDoingWhatINeedTo()
        end if
end sub

In Tickler:

sub Tickler(aTickeled as Tickeled)
        mTickeled = aTickeled
end sub


During StartMeUp, we have the following references:

      1                            2                1
mTickler <----> mTickeled <--- lv

When we exit StartMeUp(), lv goes out of scope, leaving:

      1                            1
mTickler <----> mTickeled

Then, in the action of mTickler, we say:

mTickeled.tickle()

In that call, it sets the mTickler reference to nil if IAmDone() returns true, leaving:

       0                          1
mTickler  ----> mTickeled

Doesn't this cause mTickler to vanish? And when it does, won't the remaining reference to mTickeled vanish as well? This seemed dangerous, as mTickler is no longer referenced, even though it is currently involved with the call chain.


--Brady
The La Jolla Underground




<Prev in Thread] Current Thread [Next in Thread>