问题
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
:
- 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. - 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 usingalloc
andinit
.
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