Question

I'm really struggling to understand memory leaks from a custom tableViewCell in an ARC-based project and was hoping someone might be able to shed some light on if the following might be responsible or if I'm barking up the wrong tree entirely.

So, I'm pushing a tableViewController into a navigationController and in the tableView I have my own custom tableViewCells.

This custom tableViewCell has a delegate property which is assigned from cellForRowAtIndexPath and points to the tableViewController itself, so in other words...

cell.delegate = self;

This custom tableViewCell also has a couple of observers for NotificationCenter.

Now, I've put in code to remove the observers in the tableViewCell dealloc and likewise went to add...

self.delegate = Nil;

...only to discover dealloc doesn't ever seem to be called, even when going tapping Back (i.e. going back up the navigation stack effectively finishing with the pushed tableViewContoller).

So, I guess this boils down to: could either of these aspects of the cell cause a leak? And if so, am I going about stopping the leaks in the right way?

EDIT: I'm defining my delegate property as follows:

@property (nonatomic, retain) MyTableViewController *delegate;
Was it helpful?

Solution

You need to declare your custom delegate as weak

@property (nonatomic, weak) id<MyCustomDelegateProtocol> delegate;

Otherwise you get a strong reference cycle

controller -> table view -> cell -> controller

n.b. weak is specific to ARC, if you're not using ARC you need to use assign, and make sure to nil it out yourself when you're done being its delegate.

OTHER TIPS

Another issue can be this one:

iOS 7.0 and ARC: UITableView never deallocated after rows animation

Not applicable if you're running on iOS 7.0.3+.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top