开发者

-[CALayer retain]: message sent to deallocated instance

开发者 https://www.devze.com 2023-01-07 14:39 出处:网络
I\'m developing an iphone app and when I turn on my NSZombieEnabled I have regularly a crash on error :

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


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.


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.


I would like to something more than @Tony 's answer. As it is correct.

You might be using UI object's layer or its sublayer. So just check actually UI didnot got released before using it;

if(UI object) //has instance
{
   //now use UI object.
}
0

精彩评论

暂无评论...
验证码 换一张
取 消