handle when callback to a dealloced delegate?

I implemented the delegate-callback pattern between two classes without retaining the delegate. But in some cases, the delegate is dealloced.

(My case is that I have a ViewController is the delegate object, and when the user press back button to pop that ViewController out of the NavigationController stack)

  • How to redirect back to my iOS app after logging in on FitBit login page?
  • Can two view controllers be delegates for one another?
  • How to use instance method as callback for function which takes only func or literal closure
  • Get the current view controller from the app delegate
  • How to change uitableview delete button text
  • How to perform Callbacks in Objective-C
  • Then the callback method get BAD_EXE:

    if (self.delegate != nil && [self.delegate respondsToSelector:selector]) {
        [self.delegate performSelector:selector withObject:self withObject:returnObject];

    I know the delegate-callback pattern is implemented in a lot of application. What is your solution for this?

    2 Solutions Collect From Internet About “handle when callback to a dealloced delegate?”

    Generally, a delegate should always deassociate it from its delegating object in its dealloc method. So your view controller should check in its dealloc whether it is set as the delegate of the delegating class, and if so, set the delegate property to nil.

    In most cases, I would imagine this would not be a problem because very often the delegate is the sole owner of the delegating object. So when the delegate gets deallocated, the delegating object would get deallocated too. After all, this is the reason that delegating objects usually only hold weak references to their delegates.

    I have no objective-c knowledge, but I would think that you need to test for self.delegate != nil separately e.g

    if (self.delegate != nil)
     if ( [self.delegate respondsToSelector:selector]) 
        [self.delegate performSelector:selector withObject:self withObject:returnObject];

    Oh and it’s best to reassign the delegate if it is nil.