在iOS中的MRC中,当对象设置为nil时,
myObject = nil;
被告知将发生内存泄漏,因为myObject不会指向内存地址。它之前指向的内存将丢失。因此,我们需要释放myObject,然后才能设置nil。有人可以帮助我理解,如果在ARC中将nil设置为myObject会发生什么?如果我们有这样的内容
myObject = SomeObject(value:10);
SomeObject myObject_another = myObject;
myObject = nil;
[myObject release]
时,ARC是否会呼叫myObject = nil
?[myObject_another release]
时也会调用myObject = nil
吗?[请帮助我了解ARC和非ARC之间的区别。
您可以认为,每次创建/销毁(或重新分配)新引用时,编译器插入都会保留/释放。因此它看起来像:
myObject = SomeObject(value:10); /// Memory allocated and ref count increased.
SomeObject myObject_another = myObject; /// ref count increased (now 2).
myObject = nil; /// Reassigning -> ref count decreased. SomeObject still alive.
...
/// When myObject_another is destroyed or reassigned ref count will be decreased. It's 0 now -> memory deallocated.