dpart
dpart

Reputation: 157

Test if object exists or not?

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

Answers (2)

Rafael Afonso
Rafael Afonso

Reputation: 301

When an object has been released, its retainCount is 0.

Upvotes: -1

Rob Napier
Rob Napier

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

Related Questions