触摸开始后立即执行 EXC_BAD_ACCESS
我已经开始了一个非常简单的项目来学习 iOS 编程,但在 touchesBegan
之后我得到了 EXC_BAD_ACCESS。
您可以从此处的 DropBox 下载该项目。
我基本上有一个 UIView 子类,它应该在用户触摸的地方绘制圆圈。
很简单,但我无法让它发挥作用。
非常感谢任何帮助。 谢谢!
编辑 原来问题出在 touchesBegan
: 中的这行代码
ts = [NSMutableSet setWithSet: [event touchesForView:self]];
,我改成了:
ts = [[NSMutableSet setWithSet: [event touchesForView:self]] retain];
I have started a very simple project to learn iOS programming, but I get EXC_BAD_ACCESS after touchesBegan
.
You can download the project from DropBox here .
I basically have a UIView
subclass that should draw circles wherever the user is touching.
Very simple but I cannot make it work.
Any help is highly appreciated.
Thanks!
EDIT
Turns out the problem is this line of code in touchesBegan
:
ts = [NSMutableSet setWithSet: [event touchesForView:self]];
That I changed into:
ts = [[NSMutableSet setWithSet: [event touchesForView:self]] retain];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不用ARC??? :) 很好!您不会保留您的
ts
集。请注意,setWithSet:
返回自动释放的实例。 (红色圆圈看起来很有希望:))Why don't you use ARC??? :) Its good! You do not retain your
ts
set. Note, thatsetWithSet:
returns you autoreleased instance. (The red circle looks promising :))BAD_ACCESS 错误通常与引用不再存在的对象有关(即它们被释放或自动释放)。检查
touchesBegan
或touchesEnded
内部是否存在以下问题:release
调用的任何错误放置。尝试将它们注释掉,看看错误是否消失。[NSString stringWith...]
或[NSArray arrayWith...]
,而不是使用alloc
和init
。如需进一步帮助,请尝试包含这些方法的一些代码片段。
BAD_ACCESS errors are generally related to referencing objects that no longer exist (i.e. they were deallocated or they were autoreleased). Check for these issues inside
touchesBegan
ortouchesEnded
:release
calls, when an object is still going to be used. Try commenting them out and see if the error is gone.[NSString stringWith...]
or[NSArray arrayWith...]
, instead of usingalloc
andinit
.For further help, please try to include some code snippets for these methods.
要了解 BAD_ACCESS 在 xCode 中使用 Zombie 的原因,请检查链接
http://www.markj.net/iphone-memory-debug-nszombie/
To know the reason of BAD_ACCESS use Zombie in xCode, check the link
http://www.markj.net/iphone-memory-debug-nszombie/