---
title: "[CALayer retain]: message sent to deallocated instance 0x7fb3e97a9970"
author: "BlueSkator"
date: "2014-12-31T10:13:46Z"
tags: []
categories: ["移动开发"]
slug: "calayer-retain-message-sent-to-deallocated-instance-0x7fb3e97a9970"
draft: false
lastmod: "2014-12-31T10:13:46Z"
id: 2171378
url: "https://blueskator.iteye.com/blog/2171378"
---

`-[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](https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html). 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 &gt; Analyze) menu item and see what gets reported. Pay special attention to all of the memory management errors it reports.