UIViewController not being unloaded in ViewDidUnload

The past few months I've been working in objective-C since I've been doing some contract iPhone development work. I actually really like the platform. iOS is pretty fun to code for. However, it can also be incredibly frustrating--especially coming from using Visual Studio and C#.

One of the things that stopped me for a while was that I noticed that my views were not being properly unloaded in a UINavigationController setup. Whenever I popped the view, I noticed that the retain count on it stayed above 0. If I pushed on a new view, it would add 1 to it and continue until I hit a memory error.

I knew something was holding onto it but I had no idea what--I was sending release statements to everything I retained. Then after some digging around, I discovered what was going on.

By accident, I had my custom delegates I wrote marked with (retain) in the property. This lead to one of those nasty retain cycles, which means that even though I was releasing the view, the delegate still owned it.

So I changed it to (assign) and re-loaded the app. Success! It now properly completely released and deallocated the view. Here's a quick code snippet:

@property (assign) id<MyCustomDelegate> customDelegate;


Hope this helps!

Print | posted on Tuesday, July 27, 2010 10:03 PM

Feedback

# NICE JOB.

left by G. Stokes at 2/2/2011 9:58 PM Gravatar
I was mystified by a crash that occurred when an NSNotification was sent to a controller that was long since popped off the UINavigationController's stack. I had unregistered for the notification in the controller's dealloc method, but lo and behold it was never called, and the controller's retain count after being popped stood in the teens.

I found several places where I made the mistake you mention. Although none was in the spot I expected, cleaning them all up fixed my app. THANKS FOR BOTHERING TO POST THIS.
Post A Comment
Title:
Name:
Email:
Comment:
Verification: