Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

-[CALayer retain]: message sent to deallocated instance

I'm developing an iphone app and when I turn on my NSZombieEnabled I have regularly a crash on error :

*** -[CALayer retain]: message sent to deallocated instance 0xe6012e0

It always come when I push or pop a view in my view controller. Sometimes there's this error before :

-[UIApplication endIgnoringInteractionEvents] called without matching -beginIgnoringInteractionEvents. Ignoring.

What does it mean? Anyone has a clue or has encountered this problem?

Thank you very much for help!

Romain

like image 815
Romain Piel Avatar asked Dec 04 '22 12:12

Romain Piel


2 Answers

This likely means you are trying to retain an object a UI object, such as a UIButton, that was released. There are a number of ways to track down this issue but if you can narrow down where this is occurring in your app, I generally start commenting out releases until I see where the problematic release is. My guess is you released something that was autoreleased.

like image 186
Tony Avatar answered Dec 18 '22 22:12

Tony


If the above answers did not help then check if your code is using KVO in the class where you receive this error. When KVO is sending a message to your observing class about a value on a key changing, if the class has been released by ARC then KVO will be trying to alert a non-existent address in memory about that changes, causing your app to throw this error.

Consider this, a class called MyViewController and you want to observe when the bounds property of it's view.layer changes indicating a layout change from landscape to portrait. So you add the line:

// self is MyViewController
self.view.layer.addObserver(self, forKeyPath: "bounds", options: .new, context: nil)

This will alert your class if the view size changed.

But if your viewController is dismissed, say a UINavigationController pops it off the stack, KVO is still going to try and alert MyViewController the view's bounds has changed (because now it's gone). Then when KVO does this your application is going to crash. In your debug console you would see the following message:

-[MyViewController retain]: message sent to deallocated instance

This is because you must remove the observer (MyViewController) for that keyPath. It is best to do this before MyViewController is dismissed, ike so:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.view.layer.removeObserver(self, forKeyPath: "bounds")
}

Now when you attempt to pop MyViewController from the navigation stack there will be no error.

like image 44
Brandon A Avatar answered Dec 18 '22 22:12

Brandon A