2012-02-18 125 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