2011-05-17 49 views
0

对于我的一个自定义类,我定义了一个名为initialize的方法,以便与init同时设置一些实例变量。代码如下。分析器工具报告viewDidLoad在[[Employee alloc]行上的泄漏.. 由于我释放dealloc中的变量,我认为这应该没问题。可能是什么问题?提前致谢。在iPhone中使用自定义init方法 - 识别泄漏

@interface testViewController : UIViewController <UITextFieldDelegate>{ 
    Employee *employee; 
} 
- (void)viewDidLoad { 
if(employee ==nil) 
    employee = [[Employee alloc] initialize:@"John"];  

if (![employee.entityName isEqualToString:@"Test"]) { //The leak is reported here for object allocated above 
    ///... 
} 

} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
    employee = nil; 
} 

- (void)dealloc { 
    [super dealloc]; 
    [employee release]; 

} 

//In the Employee class 
-(id) initialize:(NSString*) name{ 
    self = [super init]; 

    self.entityName = name; 


    return self; 
} 

回答

2

在你viewDidUnLoad你需要release员工以前被设置为nil。否则你的dealloc,你只是释放nil

- (void)viewDidUnload { 
    [super viewDidUnload]; 
    [employee release]; 
    employee = nil; 
} 
+0

您好,感谢,但我仍然无法删除报告泄漏。想知道为什么:( – user542584 2011-05-17 08:41:12