Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I early-release an autorelease object?

i.e. would cause the object to be released immediately and not have to be released by the pool if I did this?

[[NSArray arrayWithCapacity:100] release];

Can't find a clear explanation in the docs about this.

like image 655
Ranking Stackingblocks Avatar asked May 04 '10 23:05

Ranking Stackingblocks


People also ask

What is an Autoreleased object?

An autorelease pool stores objects that are sent a release message when the pool itself is drained. Important. If you use Automatic Reference Counting (ARC), you cannot use autorelease pools directly. Instead, you use @autoreleasepool blocks.

What is autoreleasepool swift?

The autoreleasepool allows you to explicitly manage when autorelease objects are deallocated in Swift, just like you were able to in Objective-C. Note: When dealing with Swift native objects, you generally will not receive autorelease objects.


2 Answers

It would likely crash when the object would normally be autoreleased. autorelease means "delayed release", so it will be released: just later. Since the object won't exist later as you are manually releasing it, you will likely crash due to the runtime sending the -release message to your now-deallocated object.

Edit: Note that if you -retain objects that come autoreleased, you do have to -release them: you are taking ownership.

like image 175
Grant Paul Avatar answered Nov 09 '22 13:11

Grant Paul


I realise that this is stupid now, and that I shouldn't be releasing something I don't own.

like image 42
Ranking Stackingblocks Avatar answered Nov 09 '22 13:11

Ranking Stackingblocks