[英]“[CALayer release]: message sent to deallocated instance” in UIViewController
[英]-[CALayer retain]: message sent to deallocated instance
我正在開發一個iPhone應用程序,當我打開我的NSZombieEnabled時,我經常遇到錯誤:
*** -[CALayer retain]: message sent to deallocated instance 0xe6012e0
當我在視圖控制器中推送或彈出視圖時,它總會出現。 有時候之前有這個錯誤:
-[UIApplication endIgnoringInteractionEvents] called without matching -beginIgnoringInteractionEvents. Ignoring.
這是什么意思? 任何人都有線索或遇到過這個問題?
非常感謝您的幫助!
羅曼
這可能意味着您正在嘗試將對象保留為已釋放的UI對象,例如UIButton。 有很多方法可以追蹤這個問題但是如果你可以縮小應用程序中發生這種情況的范圍,我通常會開始評論版本,直到我看到問題發布的位置。 我的猜測是你發布了一些自動釋放的東西。
我想要的不僅僅是@Tony
的回答。 因為它是正確的。
您可能正在使用UI object's layer
或其sublayer
。 所以只需檢查實際上UI在使用之前沒有被釋放;
if(UI object) //has instance
{
//now use UI object.
}
如果上述答案沒有幫助,請檢查您的代碼是否在收到此錯誤的類中使用KVO。 當KVO向您的觀察類發送關於密鑰更改的值的消息時,如果該類已被ARC釋放,那么KVO將嘗試警告內存中不存在的有關該更改的地址,從而導致您的應用程序拋出此錯誤。
考慮一下,這是一個名為MyViewController
的類,您希望觀察它的view.layer
的bounds
屬性view.layer
發生變化,指示布局從橫向更改為縱向。 所以你添加一行:
// self is MyViewController
self.view.layer.addObserver(self, forKeyPath: "bounds", options: .new, context: nil)
如果view
大小發生變化,這將提醒您的班級。
但是如果你的viewController被解雇,比如UINavigationController
它從堆棧彈出,KVO仍然會嘗試提醒MyViewController
視圖的界限已經改變(因為現在它已經消失了)。 然后,當KVO執行此操作時,您的應用程序將崩潰。 在調試控制台中,您將看到以下消息:
-[MyViewController retain]: message sent to deallocated instance
這是因為您必須刪除該keyPath的觀察者( MyViewController
)。 最好在MyViewController
被解雇之前執行此操作,因此:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.view.layer.removeObserver(self, forKeyPath: "bounds")
}
現在,當您嘗試從導航堆棧彈出MyViewController
,將不會出現任何錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.