Ranking Stackingblocks
Ranking Stackingblocks

Reputation: 607

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.

Upvotes: 5

Views: 1868

Answers (3)

Andy White
Andy White

Reputation: 88475

If you don't want the object to go into the auto-release pool, you can do a manual alloc and initWithCapabity. If you do that, you'll have to manually release it at some point.

Upvotes: 0

Grant Paul
Grant Paul

Reputation: 5902

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.

Upvotes: 7

Ranking Stackingblocks
Ranking Stackingblocks

Reputation: 607

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

Upvotes: 1

Related Questions