Eugene
Eugene

Reputation: 10045

ARC weird behavior

With ARC enabled I have a property

  @property (strong, nonatomic) NSMutableArray *arr;

I Allocate it as

  self.arr = [NSMutableArray array];

I have a custom object ERSearchView, I allocate it and add it to the arr

  ERSearchView *SV = [[ERSearchView alloc] initWithDelegate:nil];
  [arr addObject:SV];

In my search view I have overridden the dealloc method, because it seems that the search view isn't getting released:

- (void)dealloc {
  self.delegate = nil;
  NSLog(@"deallocated search view");
}

This isn't getting called when I pop my view controller. But if I nil the arr variable in the view controller's dealloc I see the log message that the search view has been deleted.

Can anyone explain how is this possible that objects inside arr aren't getting released though the view controller is 100% released since I have "did dealloc" message in its dealloc?

Upvotes: 0

Views: 149

Answers (1)

jlehr
jlehr

Reputation: 15617

Foundation collection classes such as NSArray, NSDictionary, etc. retain (or have a strong reference to, in ARC-speak) the objects they contain. So as long as the array hasn't been deallocated, the objects it refers to, including your view instance, should still be in memory.

It sounds as though you were leaking the array previously by not setting the property to nil in the controller's dealloc method. When an object declares a property as strong, it's responsible for the nilling the reference in its dealloc implementation to avoid this kind of leak.

Upvotes: 1

Related Questions