在 iPhone 中使用自定义 init 方法 - 识别泄漏
对于我的一个自定义类,我定义了一种名为“initialize”的方法,用于在 init 的同时设置一些实例变量。代码如下。分析器工具报告 viewDidLoad 中存在 [[Employee alloc].. 由于我在 dealloc 中释放了变量,我认为这应该没问题......可能是什么问题?提前致谢。
@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;
}
For one of my custom classes, I have defined a method called initialize to set some instance variables at the same time as the init. The code is below. The analyzer tool is reporting a leak in viewDidLoad on the line with [[Employee alloc]..
Since I am releasing the variable in the dealloc, I thought that this should be fine..what could be the issue? thanks in advance.
@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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的
viewDidUnLoad
中,您需要在将其设置为nil
之前release
员工。否则,在您的dealloc
中,您只是释放nil
。IE
In your
viewDidUnLoad
you need torelease
employee before it gets set tonil
. Else in yourdealloc
, you are just releasingnil
.ie