(viewConroller.view removeFromSuperview)线程:1 EXC_BAD_ACCESS(代码= 1,地址= 0x6000000008) [英] (viewConroller.view removeFromSuperview) Thread:1 EXC_BAD_ACCESS (Code=1, address = 0x6000000008)

查看:239
本文介绍了(viewConroller.view removeFromSuperview)线程:1 EXC_BAD_ACCESS(代码= 1,地址= 0x6000000008)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(^。^)对不起我的英语不好。

(^.^) Hi sorry for my English is not good.

嗨我有下一个问题我创建2个按钮第一个按钮创建一个视图控制器并将他的视图添加到其他viewcontroller第二个按钮我释放创建的视图控制器并从父视图中删除视图。
如果我点击速度更快2个按钮崩溃就像帖子标题中的消息这就是代码

Hi I have the next issue I create 2 buttons the first one button create one view controller and add his view to other viewcontroller the second button I release the view controller created and remove the view from the parent. If I tap faster the 2 buttons crash with the message like in the post Title this is the code

(我现在这是因为内存问题但是什么是问题??我这样做只是为了看看XCODE-PROFIL-INSTRUMENTS的内存管理 - 分配请帮助):

(I now this is because memory problems but what is the problem?? I do this only for see the memory managment with XCODE-PROFIL-INSTRUMENTS-Allocations please Help):

- (IBAction)create:(id)sender{
       vc = nil;
       vc = [[[VC alloc] initWithNibName:@"VC" bundle:[NSBundle mainBundle]] retain];
      [_VW addSubview:vc.view];
}

使用此按钮终止:

- (IBAction)kill:(id)sender{
      [vc.view removeFromSuperview];
      [vc release];
}


推荐答案

快速按下按钮,至少在kill按钮的情况下,它会尝试释放视图但是多次按下按钮(假设只按一次按钮时代码有效)。 EXC_BAD_ACCESS代码表示您正在尝试访问已释放的某个内存位置。

When you press the button fast, at least in the kill button's case, it attempts to release the view however many times you press the button (assuming the code works when you press the button only once). The EXC_BAD_ACCESS code means you are trying to access some memory location that has already been released.

防止这种情况的最佳方法是使用 [NSButton setEnabled :( BOOL)已启用] 方法。按下创建按钮时,启用终止按钮并禁用创建按钮。按下kill按钮时,禁用kill按钮并启用create按钮。这将防止意外的额外分配或释放。

The best way to protect against this would be to use the [NSButton setEnabled:(BOOL)enabled] method. When the create button is pressed, enable the kill button and disable the create button. When the kill button is pressed, disable the kill button and enable the create button. This will prevent accidental extra allocations or releases.

此外,在您的create方法中,您应该删除额外的 retain vc 的分配行中。 alloc 自动增加保留计数(为1),并且额外保留将其提升到2.按照它的方式,当按下kill按钮时,对象释放一次,但保留计数仍为1,造成内存泄漏。

Also, in your create method, you should remove that extra retain in vc's allocation line. alloc automatically increments the retain count (to 1), and that extra retain is bringing it up to 2. With the way it is, when the kill button is pressed, the object is released once, but the retain count is still 1, creating a memory leak.

这篇关于(viewConroller.view removeFromSuperview)线程:1 EXC_BAD_ACCESS(代码= 1,地址= 0x6000000008)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆