cessmestreet
cessmestreet

Reputation: 2318

What happens if your mark an autorelease object as autorelease

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:

 Obj * obj = [[Obj create] autorelease];

Let's say [Obj create] returns an autoreleased object.
If I add another autorelease, what happens then?

Upvotes: 7

Views: 3003

Answers (2)

cloosen
cloosen

Reputation: 993

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.

Upvotes: -2

DrummerB
DrummerB

Reputation: 40211

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.

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.

Upvotes: 5

Related Questions