xcode tool to find memory issues, access of a pointer to a location of another variable ecc
i'm using cocos2d, however the question is general. i have a class
CCNode *scaleLayer
in touchesMoved event i set
scaleLayer.scale=(some calculation)
if i compile in device the pr开发者_运维知识库ogram crashes, on simulator works well. if i put nslogs before the precedent instruction, works well even in device. could be some memory, pointer or what could it be? however, exist a tool of xcode that can make me find or understand where to point my eye? thanks
here is the code:
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
//NSLog(@"comincio");
switch ([allTouches count]) {
case 2: { //Double Touch
UITouch *t1 = [[allTouches allObjects] objectAtIndex:0];
UITouch *t2 = [[allTouches allObjects] objectAtIndex:1];
CGPoint p1=[self convertTouchToNodeSpace: t1];
CGPoint p2=[self convertTouchToNodeSpace: t2];
initialDistance = [self distanceBetweenTwoPoints:p1 B:p2];
oldScale=scaleLayer.scale;
NSLog(@"distanza iniz %f",initialDistance);
} break;
default:
break;
}
}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint location;
UITouch *t1,*t2;
CGPoint p1,p2;
NSSet *allTouces=[event allTouches];
switch ([allTouces count]) {
case 2:
//NSLog(@"2 tocchi");
t1=[[allTouces allObjects] objectAtIndex:0];
t2=[[allTouces allObjects] objectAtIndex:1];
p1=[self convertTouchToNodeSpace: t1];
p2=[self convertTouchToNodeSpace: t2];
CGFloat finalDistance=[self distanceBetweenTwoPoints:p1 B:p2];
//if i put here NSLog("%f %f %f",oldScale,finalDistance,initialDistance); all goes well
scaleLayer.scale=oldScale*finalDistance/initialDistance;
NSLog(@"scala %f",scaleLayer.scale);//finalDistance/initialDistance);
//this nslog give me error if i not put that nslog before
break;
default:
break;
}
}
-(CGFloat)distanceBetweenTwoPoints:(CGPoint)A B:(CGPoint)B{
float x=B.x-A.x;
float y=B.y-A.y;
return sqrt(x*x+y*y);
}
Try building with the static analyzer (Shift-Command-A)
Try watching the memory address. Here are some links to get you started: scottmpeak.com and SO discussion
One option you can use is zombies, it is already explained in stackoverflow, so I am not going to do it here.
But if you are getting EXC_BAD_ACCESS kind of errors you can add an exception breakpoint. That also helps sometimes. These are the steps to do so
- In the bottom left corner of the breakpoint navigator click the plus button.
- In the pop up, select "Add exception breakpoint"
- Choose the type of exception. I normally select "All" exception
- Click done.
Apple explained this here.
精彩评论