开发者

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)
{
    doSo开发者_StackOverflow中文版mething;
}

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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜