Geeks With Blogs
Create Fun Things - Samer's Developer Blog Development Blog for C#, .NET and Obj-C

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!

Posted on Tuesday, July 27, 2010 10:03 PM Quick Tips | Back to top

Comments on this post: UIViewController not being unloaded in ViewDidUnload

Requesting 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.
Left by G. Stokes on Feb 02, 2011 9:58 PM

Your comment:
 (will show your gravatar)

Copyright © samerpaul | Powered by: