A few UIViewControllers in my app that need to register with a “provider” class in their viewDidLoad methods. I’ve just been adding them to an NSMutableArray contained in the provider class.
However, I don’t want this NSMutableArray to keep them from being dealloc’ed, and I also want to have them remove themselves from the NSMutableArray in their dealloc methods.
I tried just issuing a [self release] after adding them to the array, and this works, but in order to avoid a crash when they get dealloc’ed, I have to issue a [self retain] right before I remove them. It seems like I’m doing something horribly wrong by retaining an object in it’s own dealloc method.
Is there a better way to store these values?
You just can’t do it the way you want because you would have a circular dependency. You cannot dealloc an object that is in an NSArray.
You’ll need to have them explicitly remove themselves from the array at some point in their life cycle, or just leave them there and let them get deallocated when the array gets deallocated.