1
votes

I have what seems like a weird (non?) issue with UIViewController. It appears that the controller is not releasing its subviews when it is dealloc'd. I placed NSLog messages in all of the subview's dealloc method as well as the view controller. The view controller dealloc gets called but the subview's do not. However, if I then push another instance of that view controller on to the navigation stack, it appears that all of the subviews of the previous instance are then released (I get a bunch of NSLog messages in the console letting me know). I've check and I have no separate reference to the custom view controller in the presenting view controller (the one that's doing the pushing).

One small (maybe) detail: The custom view controller does receive a block it stores and then executes before popping. However, I did send nil to it and I get the same behavior. Plus, the presenting view controller does dealloc when popped of the stack, so no retain cycle.

Also, I did try explicitly releasing each view in the dealloc method of the custom view controller. Same behavior.

Is it possible the navigation controller would be holding on to it? It doesn't seem to do this to any of my other view controllers.

My problem is that this does represent a memory leak (of all those subviews); though the leak doesn't stack, it's still a leak.

3
Minimal examples are helpful.Joe
I agree with @Joe, share some code. In particular, I've had problems with blocks leaking before. This shouldn't be a problem if you are setting it to nil unless that code path doesn't execute.Sam
Sorry about the lack of examples, but this has to do with two classes, about a total of 1,000 lines of code, and I don't know where in that code the problem is occurring.Aaron Hayman
+1 for the moral. You should put that as the answer and accept it, though, so people know the question is closed.jrturton
It won't let me post an answer to my own question yet. I will in about 5 hours :)Aaron Hayman

3 Answers

7
votes

Ok, this is embarrassing. I did find the problem in another class (called ViewDef) I was inadvertently using as a collection class. It was a quick and dirty way of keeping track of my subviews when I was first figuring out some animations (months ago). ViewDef stored frame/font/color/etc info retrieved from a database, so it was convenient to also store the views when figuring out animations (between orientations). These ViewDefs were being store by my model and passed around, so of course the views were also being retained (and replaced later by another view controller). Anyway, I forgot to insert a warning in my code to fix this later.

Moral of the story: If you plan on doing something stupid, at least document your stupidity so you don't have to broadcast it over the internet later.

0
votes

you could try setting the subviews to nil in the viewDidUnload method maybe that will help

0
votes

One thing to try is to make sure all your subviews delegates are set to nil.