Let's say I do the following (foo starts out as some object):
[foo release];
// STUFF with foo
[foo retain];
As long as I'm not re-assigning foo, at the end of this I'll still have the same foo, right? I'm not risking losing foo during the //STUFF am I?
I guess I'm making sure that my understanding of release is correct. If I release foo, it doesn't actually go away until all handles on it are gone. In other words, foo would have to be assigned to some other object in the //STUFF, or foo would have to go out of scope in the //STUFF (and presumably have a new foo created) in order for the actual original foo object to be deleted, right?
EDIT for motivation:
The reason I want to do this is that lets say I have the following switch statement:
switch (test)
{
case 1:
foo = [A alloc];
[foo inita];
break;
case 2:
foo = [B alloc];
[foo initb];
break;
case 3:
[foo setupc];
break;
case 4:
f = [D alloc];
[foo initd];
break;
}
It makes sense to release foo before the switch and retain it at then end. EXCEPT for case 3. So, I was thinking that if was safe to do what I proposed, it might make the code simpler.
Of course I can just put a release/retain pair around each alloc/init, but that's a lot of replicated code...
A [foo autorelease] and then the retain might just do the trick.