0

我正在制作一个应用程序,当你触摸一个物体时,它会移动。在它移动后,它会被释放。超过 30 次移动后,抛出了一个名为 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
    if (myselunit!=nil && myselunit.unitType != 0) {
    0x19f9e99: addl $28, %esp
    [handler.delegate performSelector:helper.touchesSel withObject:mutableTouches withObject:event]; [CCTouchDispatcher touches: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, 无,@“AppDelegate”);19 main // (内存错误) 1 0x2815: movl %eax, (%esp) 20 开始 0

  • 线程 3 0x9062690a:jae 0x9062691a;kevent + 26 0 kevent 1 libdispatch.dylib`_dispatch_mgr_thread: 2_dispatch_mgr_thread 1

    • 线程 6 0x9062583e:jae 0x9062584e;__psynch_cvwait + 26 0_psynch_cvwait 1 0x97a76ed9: movl %ebx, %ecx 4_pthread_start 1
4

1 回答 1

0

如果发生崩溃,请发布回溯。

如果您知道崩溃所在的行并且该行包含变量,请显示变量是如何初始化的。

鉴于您稍后将myselunit其视为一个对象,这很可能是因为myselunit已不正确地初始化、在此代码块之前释放或以其他方式管理不善。


作为对问题的编辑,您的评论会更好;更多的格式化机会。

  • myselunit假设非 ARC 代码,看起来您可能错误地管理的内存。

  • 请注意,您的“解除分配”代码似乎并未实际设置myselunitnil,因此导致指针悬空。

于 2012-07-07T21:45:26.313 回答