init中返回nil会不会导致内存泄露?
Will returning nil in init cause a memory leak?
在 init
中的 ARC 下返回 nil
会导致内存泄漏,当 [super init]
已经被调用,但随后返回 nil 时?这是合法的用法吗?
- (id)init {
self = [super init];
if (self) {
...
return nil;
...
}
return self;
}
我刚刚在 Instruments 中检查过 - 没有内存泄漏
首先:对于提到 ARC 的问题,他们从未阅读过 Apple 的文档,而是 clang 的。
Apple 的文档简单地隐藏(隐藏?)-init
的执行并有错误的示例:
id ref = [[Class alloc] init]
您会发现那里有类似“+alloc
转移所有权”的声明。这至少是误导性的,因为 return 值被存储在 -init
而 而不是 +alloc
的 return 值被存储。
到目前为止,clang 的文档更好、更精确。基本上他们说,+alloc
是所有权转移 和 -init
是所有权消耗和所有权转移。
http://clang.llvm.org/docs/AutomaticReferenceCounting.html#semantics-of-init
Methods in the init family implicitly consume their self parameter and return a retained object.
那么让我们看看通常的代码:
id ref = [Class alloc];
// ref is strong: RC is +1;
id ref = [ref init];
// Three things happen her:
// 1. -init is executed
// 2. The new value is stored
// 3. The old value of ref is lost
// 1. -init is executed
// self is (silently) passed to the receiver
- (id)init
{
// Since self is strong (+1), RC is +2 now
// Since -init is ownership consuming (-1), RC is +1 now
…
return self;
// Since -init is ownership transferring (+1), RC is +2 now
}
// 2. The new value is stored
// The value comes from an ownership transfer method, so the assignment to strong ref is neutral (0), RC is still +2
// 3. The old value is lost (-1), RC is +1
结果是该对象的 RC 为 +1,并且您在该代码区域中有一个对它的强引用。一切都好。 (当然有很大的优化潜力,因为在大多数情况下self
和ref
都没有改变,但让我们保持正常轨道。)
让我们在 -init
中更改 self
:
id ref = [Class alloc]; // Ownership transfer. RC is +1;
id ref = [ref init];
// Three things happen her:
// 1. -init is executed
// 2. The new value is stored
// 3. The old value of ref is lost
// 1. -init is executed
// self is (silently) passed to the receiver
- (id)init
{
// Since self is strong (+1), RC is +2 now
// Since -init is ownership consuming (-1), RC is +1 now
// Let's return nil as in your example
return nil;
// Because nil is returned, virtually the RC of nil is increased. self's RC == +1 is unchanged.
}
// 2. The new value is stored
// The new value is nil.
// However the value comes from an ownership transfer method, so the assignment to strong ref is neutral (0), RC is still +1
// 3. The old value is lost (your old ref and the self while executing -init) (-1), RC is 0
// The old object is dealloced, if you do not have another ref to it. Nothing leaks.
在 init
中的 ARC 下返回 nil
会导致内存泄漏,当 [super init]
已经被调用,但随后返回 nil 时?这是合法的用法吗?
- (id)init {
self = [super init];
if (self) {
...
return nil;
...
}
return self;
}
我刚刚在 Instruments 中检查过 - 没有内存泄漏
首先:对于提到 ARC 的问题,他们从未阅读过 Apple 的文档,而是 clang 的。
Apple 的文档简单地隐藏(隐藏?)-init
的执行并有错误的示例:
id ref = [[Class alloc] init]
您会发现那里有类似“+alloc
转移所有权”的声明。这至少是误导性的,因为 return 值被存储在 -init
而 而不是 +alloc
的 return 值被存储。
到目前为止,clang 的文档更好、更精确。基本上他们说,+alloc
是所有权转移 和 -init
是所有权消耗和所有权转移。
http://clang.llvm.org/docs/AutomaticReferenceCounting.html#semantics-of-init
Methods in the init family implicitly consume their self parameter and return a retained object.
那么让我们看看通常的代码:
id ref = [Class alloc];
// ref is strong: RC is +1;
id ref = [ref init];
// Three things happen her:
// 1. -init is executed
// 2. The new value is stored
// 3. The old value of ref is lost
// 1. -init is executed
// self is (silently) passed to the receiver
- (id)init
{
// Since self is strong (+1), RC is +2 now
// Since -init is ownership consuming (-1), RC is +1 now
…
return self;
// Since -init is ownership transferring (+1), RC is +2 now
}
// 2. The new value is stored
// The value comes from an ownership transfer method, so the assignment to strong ref is neutral (0), RC is still +2
// 3. The old value is lost (-1), RC is +1
结果是该对象的 RC 为 +1,并且您在该代码区域中有一个对它的强引用。一切都好。 (当然有很大的优化潜力,因为在大多数情况下self
和ref
都没有改变,但让我们保持正常轨道。)
让我们在 -init
中更改 self
:
id ref = [Class alloc]; // Ownership transfer. RC is +1;
id ref = [ref init];
// Three things happen her:
// 1. -init is executed
// 2. The new value is stored
// 3. The old value of ref is lost
// 1. -init is executed
// self is (silently) passed to the receiver
- (id)init
{
// Since self is strong (+1), RC is +2 now
// Since -init is ownership consuming (-1), RC is +1 now
// Let's return nil as in your example
return nil;
// Because nil is returned, virtually the RC of nil is increased. self's RC == +1 is unchanged.
}
// 2. The new value is stored
// The new value is nil.
// However the value comes from an ownership transfer method, so the assignment to strong ref is neutral (0), RC is still +1
// 3. The old value is lost (your old ref and the self while executing -init) (-1), RC is 0
// The old object is dealloced, if you do not have another ref to it. Nothing leaks.