正如 Spencer 所说,如果您在启用 ARC 的情况下进行编译,则根本无法调用release
。这样做是错误的,编译器会为您处理它。
然而:
ObjectClass *tmpObject = [[ObjectClass alloc] init];
realObject = tmpObject;
[tmpObject release]
在tmpObject
这种情况下,对于 ARC 和手动保留释放都是完全没有意义的。而且,事实上,在手动保留释放中,上面的代码会立即释放分配的对象,导致它被释放(除非ObjectClass
内部做了一些奇怪的事情),并且realObject
会留下一个悬空指针。
即,所写的代码将在任何人第一次尝试发送消息时导致崩溃realObject
。
澄清:
ObjectClass *tmpObject = [[ObjectClass alloc] init];
// tmpObject now contains a reference to an instance of ObjectClass; say, 0x12340
realObject = tmpObject;
// realObject now contains a reference to that same instance; realObject == 0x12340
[tmpObject release]
// this releases the object
// both tmpObject and realObject now reference a deallocated object; much hilarity ensues.
对于 ARC,您只需执行以下操作:
realObject = [[ObjectClass alloc] init];