Reputation: 8156
I have
static __weak ExplorerController *_rootExplorer = nil;
and
_rootExplorer = self;
NSAssert(_rootExplorer == self, @"????");
which works.
In dealloc, I try to do this
bool b = _rootExplorer == self;
which is false, but the debugger clearly states both objects are the same and have the same physical address.
When I do
long n = (long) (_rootExplorer);
long n2 = (long) (self);
I get a very large number for n2
and zero for n
. But _rootExplorer
is not nil
. However, if I do the same thing in other functions that is not dealloc
, n
and n2
are identical.
What's wrong with my code? I'm trying to keep a weak reference to one of the navigation controllers in the stack, and during unwinding, I need to release resources but only the referenced controller is allowed to do it.
Upvotes: 0
Views: 77
Reputation: 2018
Presumably you're intending to message _rootExplorer, to tell it to clean up? If so, just message it - if it is truly nil, your message will be ignored with no harm done.
And if it is truly nil, then you've probably got a bug somewhere that's causing it to be set that way prior to dealloc - perhaps the object it points to is being deallocated?
Tangentially, you may not be able to trust the debugger to get the values right, especially if you're using lldb. There are known bugs in this area. There may also be races - perhaps your _rootExplorer is being deallocated around the same time as your own object, and stopping in the debugger just happens to change the order in which that appears to happen?
Upvotes: 1
Reputation: 31026
Apple's ARC documentation says, "A weak reference is set to nil when there are no strong references to the object." So, I don't think there's anything wrong with your code (...other than, in some sense, its location being after ARC has done its work).
Upvotes: 1