EXC_BAD_ACCESS right after touchesBegan

馋奶兔 提交于 2019-12-25 02:39:07

问题


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];

回答1:


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 :))




回答2:


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.




回答3:


To know the reason of BAD_ACCESS use Zombie in xCode, check the link http://www.markj.net/iphone-memory-debug-nszombie/



来源:https://stackoverflow.com/questions/8218541/exc-bad-access-right-after-touchesbegan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!