本文介绍了在removeFromSuperview崩溃应用程序后释放UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIView 对象,我使用 [self.view addSubview:mySubView]将其添加为子视图; 。之后我调用
[mySubView removeFromSuperview]; 然后我尝试做 [mySubView release]; 但是应用程序崩溃。

I have a UIView object and I add it as a subview it using [self.view addSubview:mySubView];. Afterwards I call[mySubView removeFromSuperview]; and then I try to do [mySubView release]; but app crashes here.

RemoveFromSuperview 还调用 release mySubView

推荐答案

请发布您的代码,否则很难回答

Please post your code, else its difficult to answer

崩溃代码

UIView *subview = [[UIView alloc] init]; //manual allocation
[self.view addSubView:subview]; //view retained automatically
[subview release]; //released the manual allocated memory

for(UIView *subview in [scrollView subviews]) {

    [subview removeFromSuperview]; //released the view retained automatically. Now retain count reach 0/
   [subview release]; // crash.....
}

没有崩溃的代码

UIView *subview = [[UIView alloc] init]; //manual allocation
[self.view addSubView:subview]; //view retained automatically

[subview removeFromSuperview]; //released the view retained automatically. Now retain count reach 1

[subview release]; // No crash .retain count reach 0

你根本不发布你没有明确分配的东西或保留自己。

You simply do not release things that you have not explicitly allocated or retained yourself.

这篇关于在removeFromSuperview崩溃应用程序后释放UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 09:24