开发者

Reference not returning nil even if deallocated

I have run into a real hard one here.

My class is self-created (a + (void) start;-method) which is sent a delegate. I need to send the delegate a few messages through delegate-selectors. To check if the delegate was released, I have tried if (delegate == nil/NULL), but even if it really is released, it still says it wasn't.

How do I go around to fix this? (delegate is assigned to an id)

This is how my app is built up:

AppDelegate
> NavController
>> TableView 
>>> Post 
>>>> GetData 

GetData is the self-created class. Post is the delegate of GetData,开发者_如何学编程 and is released by TableView/NavController. After releasing Post, it is also set to nil.

In other words, GetData does not release it's delegate!


Deallocating the content of a pointer does not set the pointer to NULL automatically. You need to do it explicitly, e.g.

[delegate release];
delegate = nil;


You can't check if an object was released. Another object could've come along and occupied the same memory space, becoming indistinguishable with the original object.

The user of a class with a delegate is responsible for setting the delegate to nil when the delegate object is released. It's not detectable by the object itself.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜