开发者

Sending a message to deallocated object is working

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 Leopar开发者_JAVA百科d! Why??


    [pool drain];
    return 0;
}

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.

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


See this question.

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.


Sending a message to a deallocated instance is undefined behavior. You are not supposed to do it. It could work; it could not. Because you are not supposed to do it, they do not have to make it so that the result is consistent with your expectations.


Setting NSZombieEnabled to YES in the environment variables of your application will allow Foundation to detect this programming error, and report it as a console message which is helpful for debugging.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜