发送消息到释放的对象是工作 [英] Sending a message to deallocated object is working

查看:89
本文介绍了发送消息到释放的对象是工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Objective-C中最简单的代码导致了奇怪的行为:

The simplest code ever in Objective-C causes weird behavior:

#import Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSObject *obj = [[NSObject alloc] init];
    NSLog(@"retain count %d",[obj retainCount]);//print 1
    [obj release];//obj deallocated
    NSLog(@"retain count %d",[obj retainCount]); //still printing 1 in Snow Leopard! Why??


    [pool drain];
    return 0;
}

第二个 NSLog()应该打印消息retainCount发送到释放对象= 0x103eb0。
如果你使用Leopard,一切都很好,但在Snow Leopard中,第二个NSLog仍然打印 1

The second NSLog() should print "message retainCount sent to freed object=0x103eb0". If you use Leopard everything works fine, but in Snow Leopard the second NSLog still prints 1.

这是Xcode的Snow Leopard版本中的错误吗?

Is this a bug in the Snow Leopard version of Xcode?

推荐答案

请参阅这个问题。

See this question.

这不是一个错误。一旦释放一个保留计数为1的对象,objc运行时只需调用 dealloc ,并且不会减少保留计数。这就是它总是工作。此外,只是因为一个对象已经被发送 dealloc 并不意味着它的数据在堆上立即被销毁。它只是标记为以后销毁。

It's not a bug. Once you release an object with a retain count of one, the objc runtime simply calls dealloc and does not bother decreasing the retain count. That's how it's always worked. Also, just because an object has been sent dealloc doesn't mean its data on the heap is immediately destroyed. It's just marked for destruction later.

这篇关于发送消息到释放的对象是工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆