在 iPhone 中使用自定义 init 方法 - 识别泄漏

发布于 2024-11-08 05:33:50 字数 791 浏览 2 评论 0原文

对于我的一个自定义类,我定义了一种名为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多孤肩上扛 2024-11-15 05:33:50

在您的 viewDidUnLoad 中,您需要在将其设置为 nil 之前release 员工。否则,在您的dealloc中,您只是释放nil

IE

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

In your viewDidUnLoad you need to release employee before it gets set to nil. Else in your dealloc, you are just releasing nil.

ie

- (void)viewDidUnload {
    [super viewDidUnload];
    [employee release];
    employee = nil;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文