iPhone Development

Just another iPhone Blog

Retaining IBOutlets in iPhone is Mandatory or Is This a Bug

Its been a long time since I’ve worked on a Mac Project, But yeah today I needed to re-visit the Basics..

This post is upon “In our pre- @property era, Were we releasing the Objects created in Nib (or Xib now) i.e the one’s preceded with IBoutlet?”

I suppose, it should be released automatically when it Nib get destroyed.

However, I found out a Strange BUG which was eating up memory in one of my project.

We were referencing an UIImageView which get instantiate via Xib, and unfortunately we neither did declare any @property(retain) for this reference, nor we did release it in dealloc.

Here’s the

Hell

:
This UIImageView was holding an Image (of very large size) and when UIViewController it belongs to was getting destroyed
This one lived throughout the application lifetime, accumulating the memory.

I just somehow found out this issue (mind you, it took around 8+ hours) and resolved this by
1. adding a @property(retain) for this reference
2. Make sure I release when this associated controller gets released

This did work, and it Took me by SURPRISE!! Ask me why?

Because, as per the Theory goes, Every retain/alloc should have its corresponding release,

If you observer my solution, I just have retained the UIImageView only once and also I release it only once.

But if I don’t add this @property(retain) It is never destroyed, and STRANGEly this is not recorded as a leak.

So something’s happening there Internally, which is hidden and should be a BUG,

OR

AM I MISSING SOMETHING?

Check out the Sample Source demonstrating this BUG here

UPDATE:

More Explanation Here

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: