Emil
Emil

Reputation: 7256

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!

Upvotes: 1

Views: 206

Answers (2)

John Calsbeek
John Calsbeek

Reputation: 36497

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.

Upvotes: 3

kennytm
kennytm

Reputation: 523284

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;

Upvotes: 5

Related Questions