Basic question on retain/release semantics from Apple's reference library

Posted by davetron5000 on Stack Overflow See other posts from Stack Overflow or by davetron5000
Published on 2010-04-11T18:23:18Z Indexed on 2010/04/11 18:43 UTC
Read the original article Hit count: 268

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?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about iphone