1

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 program 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);
}
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
sefiroths
  • 1,555
  • 3
  • 14
  • 29
  • Where do you initialize the scaleLayer variable? If it isn't initialised, it might be `null` on the simulator (so nothing happens when it is messaged when you do `.scale`) but uninitialized garbage on the device (so you get a crash). – GoatInTheMachine Apr 19 '11 at 12:23
  • what is the crash log..what type of crash..bad_access? – Krishnabhadra Apr 19 '11 at 12:29

2 Answers2

0

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

Community
  • 1
  • 1
Rayfleck
  • 12,116
  • 8
  • 48
  • 74
  • scaleLayer.scale is initialized =1 in init function, and what is added as child is displayed correctly. when i try to change the scale in touchesMoved the program says _scaleX != _scaleY don't know which to return. but i change .scale, not only one, also i have seen only one time, that the variable containing the calculation to pass scale, give "nan" (i think not a number). what i think is that some pointer is accessing that area of memory changing that value inaxpectately. static analyzer don't find any issues – sefiroths Apr 19 '11 at 12:30
  • mike i have readed scottmpeak.com, but to use the malloc.c and ckheap.h i must overwrite something or just add them to my project? – sefiroths May 11 '11 at 13:25
0

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

  1. In the bottom left corner of the breakpoint navigator click the plus button.
  2. In the pop up, select "Add exception breakpoint"
  3. Choose the type of exception. I normally select "All" exception
  4. Click done.

Apple explained this here.

Community
  • 1
  • 1
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
  • tryed but scaleLayer is not a zombie, and it is never deallocated – sefiroths Apr 19 '11 at 12:49
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Monolo Aug 11 '12 at 13:24
  • 1
    Yes @Monolo I understand your concern.. I normally explain the answer here itself when I am linking to page outside either stackoverflow or official documentation.. In this case the usage of Zombies already explained in stackoverflow, so no need to explain it again.. – Krishnabhadra Aug 12 '12 at 14:04