Re: Calling -retain inside -dealloc
Re: Calling -retain inside -dealloc
- Subject: Re: Calling -retain inside -dealloc
- From: Bob Ippolito <email@hidden>
- Date: Sun, 26 Jun 2005 23:38:47 -0400
On Jun 26, 2005, at 11:19 PM, Robert McNally wrote:
Hi all! Here's the situation:
I have a class variable, an NSMutableArray, which is storing
instances
of the class. Why I'm doing this is quite complex, so I won't get
into
the details. Whenever my object's -init is called, I call
NSMutableArray's -addObject, then release my object (so that the
retain count stays the same). Inside my object's -dealloc method, I
first retain self, then call NSMutableArray's -removeObject.
So, the retain count is zero when entering the -dealloc method, then
it gets incremented to 1, then it gets decremented back to zero.
Is there any risk that when the retain count drops to zero the second
time, -dealloc will be called again, causing an endless loop?
If you just want a weak reference in your NSMutableArray, then you
should look into wrapping your instance pointer in an NSValue. See
NSValue valueWithNonretainedObject:
There's also NSMapTable and CFArray that can do this a bit more
efficiently than creating NSValues, if that matters.
-bob
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden