开发者

iphone memory management: alloc and retain properties

开发者 https://www.devze.com 2023-02-02 00:52 出处:网络
According to the docs, you do one release per alloc or retain (etc) However what about when开发者_Go百科 using retain propertys?

According to the docs, you do one release per alloc or retain (etc) However what about when开发者_Go百科 using retain propertys?

eg:

HEADER
@property(retain)UIView *someView;

IMPLEMENTATION
/*in some method*/
UIView *tempView = [[UIView alloc] init]; //<<<<<ALLOC - retain count = +1
[tempView setBackgroundColor:[UIColor redColor]];
self.someView = tempView; ///<<<<<RETAIN - retain count = +2
[tempView release];   ///should I do this?

or a different version of the IMPLEMENTATION

self.someView = [[UIView alloc] init]; //<<<<<ALLOC & RETAIN - retain count = +2
//now what??? [self.someView release]; ????

EDIT: I didn't make it clear, but I meant what to do in both circumstances, not just the first.


/*in some method*/
UIView *tempView = [[UIView alloc] init]; //<<<<<ALLOC - retain count = +1
[tempView setBackgroundColor:[UIColor redColor]];
self.someView = tempView; ///<<<<<RETAIN - retain count = +2
[tempView release];   ///should I do this? // YES!!!!

And you should also release all retain properties in your dealloc method, before [super dealloc].


Your first version is correct. There's only one ongoing reference to the view, so a retain count of 1 is appropriate.


For the second sample, you can use autorelease:

self.someView = [[[UIView alloc] init] autorelease];
0

精彩评论

暂无评论...
验证码 换一张
取 消