[CALayer retain]: message sent to deallocated instance?

烈酒焚心 提交于 2019-12-04 14:11:56

-[UIView addSubview:] causes the subview to be retained, and -[UIView removeFromSuperview] causes the view to be released. This is direct from Apple's UIView documentation. At that point, one of your objects needs to retain the CALayer or it will be deallocated.

I assumed that the CALayer was created by you directly. All UIViews are associated with CALayers; the CALayer is what a UIView draws into, and then its CALayer is composited onto the screen. If you're not working with CALayers directly, this is a symptom of another problem.

The core problem you're describing is an overrelease. It's possible you're not retaining a UIView when you should. I suggest you run the Xcode analyzer (in Xcode 4, choose the Product > Analyze) menu item and see what gets reported. Pay special attention to all of the memory management errors it reports.

I had the same problem. I was not using Interface Builder for my views. The cause was that a subview of the view that was causing the crash was being released twice (but not crashing upon the second release weirdly enough). So I would suggest you go into the view's initWithFrame method, or wherever you are adding your subviews to it and look at every release carefully. I usually do not implement the dealloc method since I am sending an autorelease message to every subview before I add it to the parent view. In this way i have less places to look at and I m sure that everything will be released once the parent view is discarded. Hope that helps...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!