Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if an object has been released before sending a message to it

I've only recently noticed a crash in one of my apps when an object tried to message its delegate and the delegate had already been released.

At the moment, just before calling any delegate methods, I run this check:

if (delegate && [delegate respondsToSelector:...]){
   [delegate ...];
}

But obviously this doesn't account for if the delegate isn't nil, but has been deallocated.

Besides setting the object's delegate to nil in the delegate's dealloc method, is there a way to check if the delegate has already been released just incase I no longer have a reference to the object.

like image 990
Tom Irving Avatar asked Jun 02 '10 00:06

Tom Irving


1 Answers

No. There is no way to tell whether a variable points to a valid object. You need to structure your program so that this object's delegate isn't going away without letting it know first.

like image 102
Chuck Avatar answered Oct 16 '22 07:10

Chuck