init方法中的[self release],[self dealloc]或[super dealloc]?

时间:2021-07-24 04:28:39

I've just been reading up on how to properly fail in an init method and the docs seem to disagree with each other. One recommends throwing an exception while the others recommend cleaning up and returning nil. What's the current best practice here?

我刚刚阅读了如何在init方法中正确失败,并且文档似乎彼此不同意。一个人建议抛出异常而其他人建议清理并返回零。目前最好的做法是什么?

4 个解决方案

#1


16  

I believe that the generally accepted practice is to return nil on failure. But you do want to release self to avoid a leak:

我相信普遍接受的做法是在失败时返回零。但是你确实希望释放自我以避免泄漏:

-(id)init
{
  if (self = [super init]) {
    ...
    if (thingsWentWrong) {
      [self release];
      return nil;
    }
    ...
  }
  return self;
}

#2


9  

The correct solutions (exceptions and/or [self release]; return nil;) having been covered, I'll address the incorrect solutions.

已经涵盖了正确的解决方案(例外和/或[自我发布];返回零;),我将解决不正确的解决方案。

Don't send dealloc directly. That's release's job. (And if your code is ever running under GC, dealloc is inapplicable, and I could only speculate on what problems calling it would cause.)

不要直接发送dealloc。这是发布的工作。 (如果您的代码在GC下运行,则dealloc不适用,我只能推测调用它会导致什么问题。)

Double-don't use super to send it directly. That would skip over your own dealloc implementation.

双 - 不要使用super直接发送它。那会跳过你自己的dealloc实现。

#3


6  

Cocoa's philosophy on exceptions is that they should only be thrown in situations that are programmer errors, like passing an illegal argument to a method. If something else goes wrong, the method should just return NO or nil, and hopefully report the details via an NSError** "out" parameter.

Cocoa关于异常的哲学是它们只应该在程序员错误的情况下抛出,比如将非法参数传递给方法。如果出现其他问题,该方法应该只返回NO或nil,并希望通过NSError **“out”参数报告详细信息。

This includes -init methods. If the error situation is something that could legitimately occur in the finished product, then the method should release self (to avoid a leak) and return nil.

这包括-init方法。如果错误情况可能在最终产品中合法地发生,那么该方法应该释放self(以避免泄漏)并返回nil。

#4


0  

The method I've always used is cleaning up and returning nil. The three methods you mention in your question title may cause segfaults higher up in the call hierarchy, whereas returning nil will not. I believe that the Apple docs themselves say to return nil on failure. Where are you finding discrepancies?

我一直使用的方法是清理并返回零。您在问题标题中提到的三种方法可能会导致调用层次结构中的段错误更高,而返回nil则不会。我相信Apple博士自己说会在失败时返回零。你在哪里发现差异?

#1


16  

I believe that the generally accepted practice is to return nil on failure. But you do want to release self to avoid a leak:

我相信普遍接受的做法是在失败时返回零。但是你确实希望释放自我以避免泄漏:

-(id)init
{
  if (self = [super init]) {
    ...
    if (thingsWentWrong) {
      [self release];
      return nil;
    }
    ...
  }
  return self;
}

#2


9  

The correct solutions (exceptions and/or [self release]; return nil;) having been covered, I'll address the incorrect solutions.

已经涵盖了正确的解决方案(例外和/或[自我发布];返回零;),我将解决不正确的解决方案。

Don't send dealloc directly. That's release's job. (And if your code is ever running under GC, dealloc is inapplicable, and I could only speculate on what problems calling it would cause.)

不要直接发送dealloc。这是发布的工作。 (如果您的代码在GC下运行,则dealloc不适用,我只能推测调用它会导致什么问题。)

Double-don't use super to send it directly. That would skip over your own dealloc implementation.

双 - 不要使用super直接发送它。那会跳过你自己的dealloc实现。

#3


6  

Cocoa's philosophy on exceptions is that they should only be thrown in situations that are programmer errors, like passing an illegal argument to a method. If something else goes wrong, the method should just return NO or nil, and hopefully report the details via an NSError** "out" parameter.

Cocoa关于异常的哲学是它们只应该在程序员错误的情况下抛出,比如将非法参数传递给方法。如果出现其他问题,该方法应该只返回NO或nil,并希望通过NSError **“out”参数报告详细信息。

This includes -init methods. If the error situation is something that could legitimately occur in the finished product, then the method should release self (to avoid a leak) and return nil.

这包括-init方法。如果错误情况可能在最终产品中合法地发生,那么该方法应该释放self(以避免泄漏)并返回nil。

#4


0  

The method I've always used is cleaning up and returning nil. The three methods you mention in your question title may cause segfaults higher up in the call hierarchy, whereas returning nil will not. I believe that the Apple docs themselves say to return nil on failure. Where are you finding discrepancies?

我一直使用的方法是清理并返回零。您在问题标题中提到的三种方法可能会导致调用层次结构中的段错误更高,而返回nil则不会。我相信Apple博士自己说会在失败时返回零。你在哪里发现差异?