请帮我找到这个iOS代码中的两个漏洞

时间:2012-02-18 12:16:55

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

在我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;

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

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

1 个答案:

答案 0 :(得分:4)

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

将其更改为self.movie = [[[myMovie alloc]init] autorelease];或在之后立即添加[self.movie release];