【问题标题】:EXC_BAD_ACESS code 1EXC_BAD_ACCESS 代码 1
【发布时间】:2012-07-07 21:05:40
【问题描述】:

我正在制作一个应用程序,当你触摸一个物体时,它会移动。在它移动后,它会被释放。超过 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, nil, @"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

【问题讨论】:

标签: objective-c exception cocos2d-iphone


【解决方案1】:

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

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

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


您的 cmets 最好作为问题的编辑;更多的格式化机会。

  • 假设您使用非 ARC 代码,您似乎可能错误地管理了 myselunit 的内存。

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

【讨论】:

  • 我以这种方式解除分配 ` [self removeChild:unit.mylabel cleanup:YES]; [rightUnits removeObject:unit]; [self removeChild:unit cleanup:YES];单位=无; ` 函数在这段代码之后运行
  • 它被初始化为 ' if (CGRectIntersectsRect(touchRect, targetRect)) { MilitaryUnits *unitclick = (MilitaryUnits *)target; myselunit=unitclick;`
  • 我确实在代码的另一部分将 myselunit 设置为 nil,并且我确实使用了 ARC 代码。
  • 然后显示myselunit的定义和崩溃的回溯
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-21
  • 1970-01-01
  • 2018-03-13
  • 2013-10-11
  • 2019-07-11
  • 2020-02-01
相关资源
最近更新 更多