我有一个洪水填充功能:
-(void) fillArea :(int) fillNum x:(int) xSpot y:(int) ySpot
{
int gridValue = 1;
int gridCount = [theGrid count];
[[theGrid objectAtIndex:(xSpot+ySpot*120)] getValue:&gridValue];
if (gridValue != 0) {
return;
}
[theGrid replaceObjectAtIndex:(xSpot + ySpot*120) withObject:[NSNumber numberWithInt:fillNum]];
[self fillArea:fillNum x:(xSpot+1) y:(ySpot)];
[self fillArea:fillNum x:(xSpot+1) y:(ySpot-1)];
[self fillArea:fillNum x:(xSpot) y:(ySpot-1)];
[self fillArea:fillNum x:(xSpot-1) y:(ySpot-1)];
[self fillArea:fillNum x:(xSpot-1) y:(ySpot)];
[self fillArea:fillNum x:(xSpot-1) y:(ySpot+1)];
[self fillArea:fillNum x:(xSpot) y:(ySpot+1)];
[self fillArea:fillNum x:(xSpot+1) y:(ySpot+1)];
return;
}
theGrid
是一个NSMutableArray
整数( a0
或 a 1
)。它只是一个一维数组,通过乘以(我的网格的宽度)来模拟二维数组。我检查了并且它等于。ySpot
120
gridCount
9600
但是,我得到一个exc_bad_access
at [[theGrid objectAtIndex:(xSpot+ySpot*120)] getValue:&gridValue]
。我检查我的xSpot
,当这种情况发生时,我每次都ySpot
知道。(xSpot+ySpot*120) < 9600
所以我知道这并不是我试图访问一个索引在我的数组之外的对象。
此外,在我的刻度函数中,我运行了代码:
int gVal = 1;
int gIndex = 0;
while (gIndex < [theGrid count]) {
[[theGrid objectAtIndex:gIndex] getValue:&gVal];
gIndex += 1;
}
我没有收到exc_bad_access
错误。请帮我弄清楚为什么我会exc_bad_access
出错。
编辑:
我拆分 [[theGrid objectAtIndex:(xSpot+ySpot*120)] getValue:&gridValue]; 进入:
id object = [theGrid objectAtIndex:(xSpot+ySpot*widthInGridSize)];
gridValue = [object intValue];
我仍然得到 exc_bad_access,它说它在线:gridValue = [object intValue];
所以我假设这意味着对象已经被释放?我不明白这怎么可能。我认为整数不需要以任何方式保留,因为它们只是整数。另外我认为将对象添加到数组会自动保留它,所以为什么我的 int 会被释放。
在调试部分中,对象的值等于: (_NSCFNumber *) 0x005aec80 (int) 0