Objective-C: Where is My Object Retained?

Fri, Aug 8

Apologies for the gap since my last post. It’s been a long week as my wife and I were in a serious car accident a week ago today. I’ll spare the details, however, let it suffice to say that our vacation plans didn’t include all the drama that unfolded that day. We are both on the mend and have plans for a full recovery. Let’s get back into it…

For those new to Objective-C and iPhone development, I want to point out something that might save you some time. Look at the code below:

@interface SomeClass : UIViewController
	UIView *containerView;
@implementation SomeClass
- (void)loadView
  UIView *uiView = [[UIView alloc] initWithFrame:
     [[UIScreen mainScreen] applicationFrame]];
  self.containerView = uiView;
  [uiView release];

In this block of code, I am setting up a container view, that will hold several subviews. Once the assignment is complete on line 8, I release the local object uiView. Question is, how I can I get away with releasing the variable and still have access to the view in the SomeClass object? In other words, where is the retain?

First thing to notice is that the dot syntax is shorthand for accessing class instance variables using Objective-C properties. If one were to write the code without using properties (and thus, no dot syntax) the answer is obvious.

For example, the equivalent code directly calling the accessor (setter) method would look as follows:

[self setContainerView:uiView];
[uiView release];

At this point, if one was curious about where the object was retained (before the release), the obvious place to look is inside the setter, which might look as follows:

- (void) setContainerView:(UIView *) view
  [view retain];
  [containerView release];
  containerView = view;

Easy enough, the object is retained as part of the setter. What we have here is a good example of pro’s/con’s when working with properties. On one hand, properties allow for simplification and consistency in our code. On the other hand, if using properties and we request the compiler to implement the getter/setter methods (through use of the @synthesize directive) there is code created for us to implement these methods, which we never see.

Although properties are no doubt handy, as this example illustrates, it’s important to understand what is happening behind the scenes. A clear picture of the little nuances can go a long ways when it comes to debugging a thorny problem.


I’m sorry to hear about your accident, I pray that both of you heal quickly. Just thought I’d drop you a line to say that I enjoy your site, thanks for the tips! Keep up the great work!

by Jon Buys on Aug 8, 2008. #

Thanks Jon, I appreciate you thinking of us and glad you’ve enjoyed the tips!

by john on Aug 8, 2008. #

Glad to hear that you are still alive! I hope you and your wife have nothing left from the accident.

by Zettt on Aug 10, 2008. #

Just to add…

If you do use properties with the @property directive you can specify the declaration attributes. In this case you could use:

@property (retain) UIView *containerView

This will tell the compiler to use retain the value passed in when synthesizing the accessors. The default is assign, which implements accessors which assign (using =) the values passed. This makes it easier to know when you can release objects passed into accessors.

by stompy on Aug 14, 2008. #

Stompy, thanks for the comment. I took the liberty to move the comment over to the iPhone Developer Tips blog as well (as I copied this post to that blog as well).



by john on Aug 14, 2008. #

Comments are closed on this post. You can comment on this same post which has been moved to the iPhone Developer Tips blog: http://iphonedevelopertips.com/objective-c/where-is-my-object-retained.html

by john on Aug 16, 2008. #