EXC_BAD_ACESS code 1

问题描述:

我正在制作一个应用程序,当你触摸一个物体时,它会移动。移动后,它会被释放。经过30多次移动之后,会抛出名为EXC_BAD_ACESS且CODE = 1的异常。为什么抛出异常?EXC_BAD_ACESS code 1

if (myselunit!=nil) { 
       touch = [touches anyObject]; 
       location = [touch locationInView:[touch view]]; 
       location = [[CCDirector sharedDirector] convertToGL:location]; // Locks on to touch's coordinates 
       realx = location.x; 
       realy = location.y; 
       realDest = ccp(location.x, location.y); 
       int offRealX = realx - myselunit.position.x; // exception occurs here 
       int offRealY = realy - myselunit.position.y; 
       float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY)); 
       float velocity = 50*myselunit.speed; // 480pixels/1sec 
       float realMoveDuration = length/velocity; 
       [myselunit runAction:[CCSequence actions:[CCMoveTo actionWithDuration:realMoveDuration position:realDest],[CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)],nil]]; 

       //    [myselunit runAction:[CCSequence actions:[CCCallFuncN actionWithTarget:self selector:@selector(staminaUse:)],nil]]; 

       NSLog(@"%f",location.x); 
       myselunit = nil; 

      } 

这是myselunt回流 错误列表:

倒流:

  • 线程1
    0x1fa509f:MOVL(%EDI),%ESI
    如果(myselunit! =无& & myselunit.unitType!= 0){
    0x19f9e99:addl $ 28,%esp
    [handler.delegate performSelector:helper.touchesSel withObject:mutableTouches withObject:event]; [CCTouchDispatcher触及:withEvent:withTouchType:] 1 [self touches:touches withEvent:event withTouchType:kCCTouchBegan]; 4 - [CCTouchDispatcher touchesBegan:withEvent:] 1 [touchDelegate_ touchesBegan:touches withEvent:event]; 5 - [EAGLView的touchesBegan:withEvent:方法] 1 0x8562cf:MOVL 6769896(%ESI),%eax中6 - [UIWindow_sendTouchesForEvent:] 1 0x82e626:xorl%eax中,%eax中18 UIApplicationMain 1 INT retVal的= UIApplicationMain(的argc,argv的,nil,@“AppDelegate”); 19主//(存储器错误)1 0x2815:MOVL EAX%,(%ESP)20开始0

  • 线3 0x9062690a:宰0x9062691a; KEVENT + 26 0 1 KEVENT libdispatch.dylib`_dispatch_mgr_thread:2_dispatch_mgr_thread 1

    • 螺纹6 0x9062583e:宰0x9062584e; __psynch_cvwait + 26 0_psynch_cvwait 1 0x97a76ed9:MOVL%EBX,ECX%1 4_pthread_start
+1

你应该大部分代码。 instanciate/deallocate myselunit在哪里? – 2012-07-07 21:36:16

+0

调试101:http://www.learn-cocos2d.com/2011/10/xcode-4-debugging-crashcourse/和http://www.raywenderlich.com/10209/my-app-crashed-now-what -part-1 – LearnCocos2D 2012-07-08 11:59:09

如果你有一个碰撞,后回溯。

如果您知道崩溃的那一行,并且该行包含变量,则显示变量如何初始化。

既然你在后面治疗myselunit作为一个对象,它是最有可能的,因为myselunit已不正常初始化,之前的这段代码发布,或以其他方式处置不当。


您的意见将是更好的编辑问题;格式化的机会更多。

  • 它看起来像你很可能管理myselunit的内存不正确,假设非ARC代码。

  • 请注意,您的“取消分配”代码似乎没有将myselunit设置为nil,从而导致悬挂指针。

+0

我以这种方式取消分配 '[self removeChild:unit.mylabel cleanup:YES]; [rightUnits removeObject:unit]; [self removeChild:unit cleanup:YES]; unit = nil; ' 功能的代码 – TheLivingForce 2012-07-08 00:54:55

+0

这个块它被初始化为例如 ' 后运行如果(CGRectIntersectsRect(touchRect,targetRect)){ MilitaryUnits * unitclick =(MilitaryUnits *)目标; myselunit = unitclick;' – TheLivingForce 2012-07-08 00:58:21

+0

我在代码的另一部分设置myselunit为零,而且我使用ARC代码。 – TheLivingForce 2012-07-08 20:54:36