【问题标题】:Please help me find the two leaks in this iOS code请帮我找出这个 iOS 代码中的两个漏洞
【发布时间】:2012-03-09 14:37:40
【问题描述】:

在我的viewController.m 我有这个代码:

self.movie = [[myMovie alloc]init];
self.movie.name = @"Iron man 2"; \\this line leaks

...

nameLbl = [[UILabel alloc] initWithFrame:CGRectMake(30, 20, 200, 20)]; \\this line leaks
nameLbl.backgroundColor = [UIColor clearColor];

viewController.h我有这个代码:

@interface ViewController : UIViewController

{
    myMovie * movie;

    UILabel * nameLbl;
}

@property (nonatomic, retain) myMovie * movie;
@property (nonatomic, retain) UILabel * nameLbl;

还有 myMovie.h:

{
    NSString* name;
}

@property (nonatomic, retain) NSString* name;

我的电影.m:

#import "myMovie.h"

@implementation myMovie
@synthesize name, gross, desc;



-(void) dealloc
{
    self.name = nil;
    self.gross = nil;
    self.desc = nil;

    [super dealloc];
}

@end

当然这只是必要的代码。我无法弄清楚为什么它会泄漏。我不知道这是否是原因,但我的应用程序崩溃了。

【问题讨论】:

  • 那条线路泄漏是什么告诉你的?

标签: objective-c memory-leaks ios5 ios-simulator


【解决方案1】:

泄漏的那一行是上面那一行:self.movie = [[myMovie alloc]init];

将其更改为self.movie = [[[myMovie alloc]init] autorelease]; 或添加[self.movie release]; 作为紧随其后的行。

【讨论】:

  • 我做了 Product->Analyse ,你建议修复第一个泄漏,谢谢!关于第二次泄漏有什么想法吗?
  • 对 nameLbl 执行相同操作。为这些变量设置的值由合成的 setter 方法保留,因为它们是用“retain”属性声明的。你传入的值应该是自动释放的。
  • 我是这样做的:nameable = [[[UILabel alloc] initWithFrame:CGRectMake(30, 20, 200, 20)] autorelease];它仍然泄漏
  • @nir -- 试试self.nameLbl = [[[... autorelease];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多