Can I early-release an autorelease object?
Asked Answered
A

3

5

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.

Altruist answered 4/5, 2010 at 23:54 Comment(0)
W
7

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.

Wellbeing answered 4/5, 2010 at 23:57 Comment(3)
Let's say that (for whatever reason) I do want it to get released right now. Is "[[obj retain] release]" a valid option?F
Not really. In that case, you should wrap the whole thing in @autoreleasepool { } to create and destroy the pool the autorelease is happening in.Wellbeing
agree, wrap the whole thing in @autoreleasepoolHexamethylenetetramine
A
1

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

Altruist answered 4/5, 2010 at 23:56 Comment(0)
L
0

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.

Lifetime answered 5/5, 2010 at 0:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.