触摸开始后立即执行 EXC_BAD_ACCESS

发布于 2024-12-17 08:00:09 字数 497 浏览 4 评论 0原文

我已经开始了一个非常简单的项目来学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

廻憶裏菂餘溫 2024-12-24 08:00:10

为什么不用ARC??? :) 很好!您不会保留您的 ts 集。请注意,setWithSet: 返回自动释放的实例。 (红色圆圈看起来很有希望:))

Why don't you use ARC??? :) Its good! You do not retain your ts set. Note, that setWithSet: returns you autoreleased instance. (The red circle looks promising :))

萌酱 2024-12-24 08:00:10

BAD_ACCESS 错误通常与引用不再存在的对象有关(即它们被释放或自动释放)。检查 touchesBegantouchesEnded 内部是否存在以下问题:

  1. 当仍要使用对象时,release 调用的任何错误放置。尝试将它们注释掉,看看错误是否消失。
  2. 使用自动释放的对象而不保留它们。自动释放的对象是使用直接引用类的方法名称创建的对象,例如:[NSString stringWith...][NSArray arrayWith...],而不是使用 allocinit

如需进一步帮助,请尝试包含这些方法的一些代码片段。

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 or touchesEnded:

  1. Any incorrect placement of release calls, when an object is still going to be used. Try commenting them out and see if the error is gone.
  2. Using autoreleased objects and not retaining them. Autoreleased objects are the ones created using the method names that refer directly to the class, like these: [NSString stringWith...] or [NSArray arrayWith...], instead of using alloc and init.

For further help, please try to include some code snippets for these methods.

深海蓝天 2024-12-24 08:00:10

要了解 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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文