If class A is using class B and class A is class B\'s delegate, is it ok if the delegate is set to nil in class B\'s dealloc? I have seen code usually resetting the delegate
First, a few observations...
[super dealloc]
at the end of your own dealloc method.-dealloc
, since 'b' is about to be destroyed anyhow. If it's possible that other objects have a reference to 'b' (meaning it might outlive 'a') then set the delegate to nil.self.b.delegate = self
is equivalent to [[self getB] setDelegate:self]
— it's just syntactic sugar that makes it look like you're accessing the ivar directly, but you're actually not.self.b
retains the value (the property is set to "assign"), you have a memory leak on your hands.Here's how I would probably write it:
- (void) someFunc {
b = [[B alloc] init];
b.delegate = self; // or [b setDelegate:self];
}
- (void) dealloc {
b.delegate = nil;
[b release];
[super dealloc];
}