adit
adit

Reputation: 33644

setting delegate to nil to "avoid message sent to deallocated instance"

If you have a view controller that implements a delegate, then should you set the delegate to nil when it's deallocated? I get this message all the time.

I am not sure by saying "setting the delegate to nil", what does this mean? Can someone give me a simple example to clarify this?

Upvotes: 2

Views: 1104

Answers (2)

Claus Broch
Claus Broch

Reputation: 9212

It's always a good idea to set the delegate to nil before releasing the controller.

It might not be deallocated right away and may accidentally call back to your code which may or may not have gone out of scope -> crash.

Upvotes: 1

Sascha
Sascha

Reputation: 5973

You most likely just have to do "SomeObject.delegate = nil" before deallocating SomeObject's old delegate. Can you provide any code snippets from where this warning occurs?

Upvotes: 1

Related Questions