4

After upgrading my app to iOS9 I’am getting an error in my app which says:

: objc[344]: Cannot form weak reference to instance (0x15919e00) of class LoginVC. It is possible that this object was over-released, or is in the process of deallocation.

Below is the function in which i get this error:

-(void)dismissLogin {
self.isLoggingIn = NO;
[self stopLoginAnimation];
[self dismissViewControllerAnimated:YES completion:NO];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self.appDelegate setLoginVC:nil];
[self.view removeFromSuperview];
//[APPDEL selectTabBar];
}

The app gets stuck at the login screen and doesn't switch to next screens.

This error doesn’t come in iOS8. Can anyone help me with this issue.

eureka19
  • 1,661
  • 1
  • 18
  • 30
  • Have you tried using the debugger while running your app to check your logic? Does this error occur at any particular line in your app (in which case post some code)? – Robotic Cat Sep 29 '15 at 11:47
  • This error is coming only on the device and not on the xcode. I'm setting up my developer licence to debug from the device. Do you have any idea why this is happening? – eureka19 Sep 29 '15 at 13:57
  • I have no idea and without code to look at I would be guessing. However, the implication is your logic around your object graph is wrong. Somewhere LoginVC is being deallocated when you want it to be alive. This is a logic problem but you will need to debug where it is happening. Log in the `dealloc` method of your classes to understand when your objects are being destroyed. – Robotic Cat Sep 29 '15 at 14:45
  • This is probably related to the inexplicable change in behavior in iOS 9+/ OS X 10.11+ where [assigning an object undergoing deallocation to a weak variable crashes](https://stackoverflow.com/questions/35991363/why-setting-object-that-is-undergoing-deallocation-to-weak-property-results-in-c) – user102008 Feb 09 '17 at 20:58

2 Answers2

4

Make sure you are not using instance being deallocated.

I have the same issue. It was not occurring in iOS 8 but occurred in iOS 9. Because I was overriding setDelegate method like this.

-(void)setDelegate:(id<UICollectionViewDelegate>)delegate{
    _internalDelegate = delegate;
    [super setDelegate:self];
}

So in iOS 9, OS sets delegate to nil on de-allocation, but I was setting it to self. So quick fix was

-(void)setDelegate:(id<UICollectionViewDelegate>)delegate{
    _internalDelegate = delegate;
    if (delegate) {
        //Set delegate to self only if original delegate is not nil
        [super setDelegate:self];
    }else{
        [super setDelegate:delegate];
    }
}
Muhammad Nabeel Arif
  • 18,528
  • 8
  • 48
  • 70
  • 1
    For me the error is only coming on the iOS 9 device and not on iOS 9 simulator. I don't get how it's working on the simulator if there is some memory issues in my code. – eureka19 Sep 30 '15 at 12:27
0

I ran into this issue recently and this helped me come to the conclusion that I did. The only issue I have with the solution provided above is that if you need the subclass to gain functionality even when its internalDelegate is nil, it just won't work.

Here's the solution I came up with that both prevents the crash and allows functionality to exist even with a nil internalDelegate. Figured I'd share in case anyone else came across this.

  1. Create a second internal property, I called this weakSelf
@property (nonatomic, weak) LoginVC *weakSelf;
  1. Inside any initialization methods, set weakSelf to self
 - (id)init {
    if ((self = [super init])) {
        self.weakSelf = self;
    }
}
  1. Update delegate method
- (void)setDelegate:(id)delegate {
    _internalDelegate = delegate;
    [super setDelegate:self.weakSelf];
}
jordanperry
  • 3,348
  • 2
  • 17
  • 12