对象泄露:以后不引用已分配的对象

时间:2012-01-20 17:27:50

标签: iphone

当我分析它时收到这些消息:

Method返回一个Objective-C对象,其下面的语句带有+1保留计数

self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

以后在此执行路径中不会引用对象泄漏的已分配对象,并且保留计数为+ 1

[self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

任何人都知道如何修复这些消息。

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

假设view是具有retain属性self.view的属性,则保留视图,以便initWithFrame创建的保留是需要释放的额外保留。 / p>

简化autorelease

UIView *newView = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 

更好的是,如果可能的话,使用ARC。 ARC适用于iOS 4.x及更高版本,并且可以逐个文件地用于混合实现。然后,应用中没有retainreleaseautorelease来电。

答案 1 :(得分:1)

self.view是@property,在设置时会保留。你需要发布它。

尝试:

UIView *newView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
self.view = newView; 
[newView release];

self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];