I have done Objective-C way back when, and have recently (i.e. just now) read the documentation on Apple’s site regarding the use of retain and release. However, there is a bit of code in their Creating an iPhone Application page that has me a bit confused:
- (void)setUpPlacardView
{
// Create the placard view -- it calculates its own frame based on its image.
PlacardView *aPlacardView = [[PlacardView alloc] init];
self.placardView = aPlacardView;
[aPlacardView release]; // What effect does this have on self.placardView?!
placardView.center = self.center;
[self addSubview:placardView];
}
Not seeing the entire class, it seems that self.placardView is also a PlacardView * and the assignment of it to aPlacardView doesn’t seem to indicate it will retain a reference to it. So, it appears to me that the line I’ve commented ([aPlacardView release];) could result in aPlacardView having a retain count of 0 and thus being deallocated. Since self.placardView points to it, wouldn’t that now point at deallocated memory and cause a problem?
Hi, Obj-C introduced an (evil) concept of properties in the meantime. Note that
and
is different. The former, by definition, calls
[self setPlacardView:xxx]whereas the latter just assignsxxxinto a member.Now, when you look at MoveMeView.h, you see the line
and in MoveMeView.m
These tell the compiler to generate
-setPlacardView:andplacardViewappropriately, using the standard retain/release semantics. For more details, see Apple’s documentation for properties.