If I do this-
SomeObject* someObject = [[SomeObject alloc] init];
[[someObject release] release];
I presume my program wouldn’t crash because the second over-release is acting on nothing.
Whereas
[[someObject autorelease] autorelease];
would crash because the object is released twice by being passed down the chain.
This is a matter of curiousity only, I guess I’ve never had a problem with it so far.
(See this answer for an explanation what
onewaydoes.)Your first example doesn’t even compile, as
void!=nil. But if you simply callreleasetwo times on an object that has a retain count of 1, it’ll for sure crash.As for why
releasereturnsvoidwhileautoreleasereturnsid: The first says I don’t need this object any more, you may get rid of it. Thus, if it would return something and you would operate on it you’re likely to operate on a dead object. By contrast,autoreleasemore or less says “Soon I won’t be needing the object any more but let it stick around until I’m done with the current method.”So as you see they are similar in that they say I won’t be needing this object any more, but they differ in the time:
releasesays “you can get rid of the object now“, whileautoreleasesays “you may get rid of the object later“.