フラッドフィル機能があります:
-(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;
}
theGridNSMutableArrayintのです ( a0または a 1)。(グリッドの幅)を掛けて2D配列をシミュレートするのは、単なる1D配列です。を確認したところ、と同じです。ySpot120gridCount9600
ただし、exc_bad_accessatを取得し[[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 は単なる int であるため、保持する必要はまったくないと思いました。また、配列にオブジェクトを追加すると自動的に保持されると思ったので、なぜintが解放されるのでしょうか。
デバッグ セクションでは、オブジェクトの値は等しいと言われています: (_NSCFNumber *) 0x005aec80 (int) 0