My question may sound stupid an all, but I like to know what happens if I mark an autoreleased object as autorelease. Will it be released twice? Or nothing happens? For example:
我的问题可能听起来很愚蠢,但是我想知道如果我把一个autorelease标记为autorelease会发生什么。它会被释放两次吗?还是什么都没发生?例如:
Obj * obj = [[Obj create] autorelease];
Let's say [Obj create]
returns an autoreleased object.
If I add another autorelease, what happens then?
假设[Obj create]返回一个自动上传的对象。如果我再添加一个自动释放酶,会发生什么?
2 个解决方案
#1
5
Yes, sending autorelease
twice will release the object twice. If your create
method returns an autoreleased object and you send another autorelease
message to it, your app will crash, because you'll be releasing a deallocated object.
是的,两次发送autorelease将会释放两次。如果您的创建方法返回一个自动的对象,并且您发送另一个autorelease消息到它,您的应用程序将崩溃,因为您将释放一个deallocation对象。
Having said that, why don't you use the new Automatic Reference Counting (ARC)? You don't have to worry about (auto)releasing objects anymore.
既然如此,为什么不使用新的自动引用计数(ARC)呢?你不必再担心(自动)释放对象了。
#2
-2
You use the Class Method(+), you should not to care the memory. People use Class Method one reason is that it can return an autorelease object. If you release or autorelease the object which the Class Method returns, it will crash.
使用Class方法(+)时,不应该关心内存。人们使用类方法的一个原因是它可以返回一个自动发出的对象。如果释放或自动释放类方法返回的对象,它将崩溃。
#1
5
Yes, sending autorelease
twice will release the object twice. If your create
method returns an autoreleased object and you send another autorelease
message to it, your app will crash, because you'll be releasing a deallocated object.
是的,两次发送autorelease将会释放两次。如果您的创建方法返回一个自动的对象,并且您发送另一个autorelease消息到它,您的应用程序将崩溃,因为您将释放一个deallocation对象。
Having said that, why don't you use the new Automatic Reference Counting (ARC)? You don't have to worry about (auto)releasing objects anymore.
既然如此,为什么不使用新的自动引用计数(ARC)呢?你不必再担心(自动)释放对象了。
#2
-2
You use the Class Method(+), you should not to care the memory. People use Class Method one reason is that it can return an autorelease object. If you release or autorelease the object which the Class Method returns, it will crash.
使用Class方法(+)时,不应该关心内存。人们使用类方法的一个原因是它可以返回一个自动发出的对象。如果释放或自动释放类方法返回的对象,它将崩溃。