Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 838
cessmestreet Avatar asked Sep 18 '12 23:09

cessmestreet


People also ask

What does Autorelease mean?

Filters. (computing) Automatic release (of previously allocated resources)

How retain object in objective c?

The system that Objective-C uses is called retain/release. The basic premise behind the system is that if you want to hold on to a reference to another object, you need to issue a retain on that object. When you no longer have a use for it, you release it. Similar to Java, each object has a retain count.

What method is automatically called to release objects in objective c?

The NSArray class method array returns a newly initialized array that is already set for autorelease. The object can be used throughout the method, and its release is handled when the autorelease pool drains. At the end of this method, the autoreleased array can return to the general memory pool.


1 Answers

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.

like image 67
DrummerB Avatar answered Nov 09 '22 19:11

DrummerB