自动释放一个对象两次 [英] Autoreleasing twice an object
问题描述
NSString *str = [[[[NSString alloc]init]autorelease]autorelease];
str = @"hii";
NSLog(@"%@",str);
任何人都可以帮我讲一下这段代码吗?自动释放对象两次,将发生什么情况.当我运行代码时,我没有任何僵尸.为什么会这样.
Can any one help me to tell about this code. Autoreleasing the object twice what will happened. When i run the code i didn't get any zombie. why it so.
推荐答案
在销毁自动释放池时,该对象被释放两次,这很可能在运行循环迭代的末尾.为什么不会崩溃,是NSString
在某些实例中返回单例,例如您创建的空字符串或字符串文字(您应该不依赖它,这就是当前发生的事情!) ,这些对象将不会被释放,这就是为什么您没有僵尸的原因.
The object gets released twice when the autorelease pool is destroyed, which is probably going to be at the end of the run loop iteration. Why it doesn't crash is, that NSString
returns singletons for some instances, for example the empty string you create or string literals (you should NOT depend on it, thats just what currently happens!), these objects won't be deallocated and this is why you don't get a zombie.
这篇关于自动释放一个对象两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!