overt and covert retain-release question (instigated by UIViewController on iPhone)
overt and covert retain-release question (instigated by UIViewController on iPhone)
- Subject: overt and covert retain-release question (instigated by UIViewController on iPhone)
- From: Stuart Malin <email@hidden>
- Date: Wed, 12 Nov 2008 10:00:48 -0800
First: sorry if iPhone questions now belong elsewhere, but I am
posting here because I want to validate my understanding of retain-
release in the Cocoa context. I've just acquired the book "The
iPhone Developer's Cookbook" by Erica Sadun. Her first "Hello World"
example (listing 1-4, if you have the book) troubles me. This example
piques for me several questions regarding proper coding regarding
retain-release, as well as a curious question regarding KVO.
In the code, Sadun subclasses the UIViewController class, and in its -
loadView method, has the following (partial code):
- (void) loadVew
{
contentView = [[UIImageView alloc] initWithFrame........];
...
self.view = contentView;
[contentView release];
...
}
What troubles me is the alloc-init of the contentView, its assignment
to a property of the UIViewController class (self.view), and the
allocated UIImageView object's immediate release. It seems to me that
the allocated instance will be deallocated at some subsequent time,
yet the view property (of the UIViewControllerClass) has been set to
a reference to that object. If I understand retain-release management
correctly, this could lead to a memory exception (I'd expect the the
de-allocation to occur long before the application terminates). I
could understand releasing the alloc'd object if it there was a
method that set the view property (for then we could presume that the
setter would retain the passed in object), and that had been called.
But here there is only an assignment.
Is the iPhone UIKit substantially different in regard to how this
works? Or is my understanding of what happens in the above scenario
incorrect? Or is it possible that the view property is KVO observed,
and when it is assigned to, some code further retains the new value
(and presumedly releases the old). iPhone aside, would the notion I
put forth (of observing a value change, and when it did, release a
prior value and retain a new value) be considered good or bad
programming practice (in the context of Cocoa KVO)? [this is what I
mean by "covert" retain-release]
I do have one other hypothesis: this code example works
coincidentally because once the contentView is presented on-screen,
the app has no other function that would cause the display to change,
and hence the object is never again accessed.
_______________________________________________
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