Reputation: 157
Foo*foo1;
foo1=[[Foo alloc]init];
After release, object foo1
still points to some memory but the object doesn't exist. I want to do something like this:
if (foo1)
{
doSomething;
}
I cannot assign a nil
because maybe this object exists and maybe I will get a leak if assign it to nil
.
How can I get to know whether object exists or not?
Upvotes: 0
Views: 629
Reputation: 299345
Whenever you release an object, you should set your pointer to nil. In your case:
Foo *foo1 = [[Foo alloc] init];
... doing stuff with foo1 ...
[foo1 release], foo1 = nil;
The assignment of foo1 = nil
does not modify the object in any way. It clears your pointer to the object. Since you have released your retain on the object, obviously you do not care about it any more, and so you should clear your pointer to it.
Upvotes: 2