我正在创建一个应用,当你触摸一个物体时,它会移动。移动后,它会被取消分配。经过三十多次移动后,抛出一个名为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触及: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:jae 0x9062691a; kevent + 26 0 kevent 1 libdispatch.dylib`_dispatch_mgr_thread:2_dispatch_mgr_thread 1
答案 0 :(得分:0)
如果您遇到了崩溃,请发布回溯。
如果您知道崩溃线并且该行包含变量,请显示变量的初始化方式。
鉴于您稍后将myselunit
视为对象,很可能是因为myselunit
未正确初始化,在此代码块之前发布,或者其他方式管理不善。
对于问题的编辑,您的评论会更好;更多格式化的机会。
假设非ARC代码,您可能错误地管理myselunit
的内存。
请注意,您的“取消分配”代码似乎并未将myselunit
实际设置为nil
,从而导致悬空指针。