Re: overt and covert retain-release question (instigated by UIViewController on iPhone)
Re: overt and covert retain-release question (instigated by UIViewController on iPhone)
- Subject: Re: overt and covert retain-release question (instigated by UIViewController on iPhone)
- From: Peter N Lewis <email@hidden>
- Date: Thu, 13 Nov 2008 10:17:29 +0900
- (void) loadVew
{
contentView = [[UIImageView alloc] initWithFrame........];
...
self.view = contentView;
[contentView release];
...
}
As Luke said, you need to read the Objective C 2.0 language section
on properties.
First off, you should understand that
self.view = contentView;
is just syntactic sugar for:
[self setView:contentView];
The import thing to note is this is a *method call* not an ivar
(instance variable) assignment which is what it deceptively looks
like. This is especially important to understand as
view = contentView;
would be an ivar assignment, although this would not work in your
case since view is not actually an ivar in your sub class.
Since the view property is defined as:
@property(nonatomic, retain) UIView *view
this means the setView method retains its parameter when it assigns
to the ivar, something conceptually like:
- (void) setView: (UIView*) theNewView;
{
if ( theNewView != _view ) {
[theNewView retain];
[_view release];
_view = theNewView;
}
}
So what is happening in loadView is:
contentView = [[UIImageView alloc] initWithFrame........];
contentView is allocated, and has a retain count of 1
self.view = contentView;
contentView is retained (2) and stored in the _view ivar
[contentView release];
contentView is released (retain count of 1) to balance the [UIImageView alloc].
In the dealloc method of UIViewController, it will do the equivalent of:
self.view = nil;
which will release the _view ivar which will then deallocate the UIImageView.
There are several critical things to understand:
+ self.view = blah is a method call, not an assignment.
+self.view is responsible for looking after the ownership of the _view ivar.
+ loadView created the UIImageView, so after it is done with it
(regardless of what anyone else wants to do with it) it needs to
release/autorelease it. An exception would be if it was named with
copy/create/new in which case it returns it without releasing it.
It is a bit confusing getting used to properties, but after a short
while it becomes second nature (primarily because it is so
self-consistent), and then if you have to go back to programming
without them it is a real pain!
HTH!
Peter.
--
Keyboard Maestro 3 Now Available!
Now With Status Menu triggers!
Keyboard Maestro <http://www.keyboardmaestro.com/> Macros for your Mac
<http://www.stairways.com/> <http://download.stairways.com/>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden