2012-02-18 92 views
1

在我viewController.m我有这样的代码:请帮我看看这个iOS的代码,这两个泄漏

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; 

and myMovie.h:

{ 
    NSString* name; 
} 

@property (nonatomic, retain) NSString* name; 

myMovie.m:

#import "myMovie.h" 

@implementation myMovie 
@synthesize name, gross, desc; 



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

    [super dealloc]; 
} 

@end 

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

+0

什么告诉你该线路泄漏? – 2012-02-18 13:07:54

回答

4

多数民众赞成泄漏线上面的一个:self.movie = [[myMovie alloc]init];

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

+0

我做了产品 - >分析,你建议修复第一次泄漏,谢谢!关于第二次泄漏的任何想法? – Nir 2012-02-18 13:14:58

+0

对nameLbl做同样的处理。设置为这些变量的值由合成的setter方法保留,因为它们是用“retain”属性声明的。您传入的值应该是自动释放的。 – bneely 2012-02-18 13:17:02

+0

我这样做:nameable = [[[[UILabel alloc] initWithFrame:CGRectMake(30,20,200,20)] autorelease];它仍然泄漏 – Nir 2012-02-18 13:21:51