IOS : Distribution (release) configuration bugs and crashes
I've created an app to solve sudoku grids...but here is my problem : my app has some bug and even crashes with my Distribution Configuration for compiling (equivalent to Release Configuration).
Here is the code where it bugs :
- (int*)completeNb:(int[9][9])aMatrix {
int n, nb;
int cell[2];
int *pcell;
BOOL found = FALSE;
BOOL pos = FALSE;
for (int k = 1; k <= 9; k++) {
n = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (aMatrix[i][j] == k)
n++;
}
}
if (n == 8) {
found = TRUE;
nb = k;
开发者_开发百科 break;
}
}
if (found) {
for (int l = 0; l < 9; l++) {
for (int m = 0; m < 9; m++) {
if ([self isPossibleValue:aMatrix value:nb line:l column:m]) {
cell[0] = l;
cell[1] = m;
pos = TRUE;
break;
}
}
if (pos)
break;
}
pcell = cell;
}
else {
pcell = nil;
}
return pcell;
}
I have a special sudoku grid where I should find {6,6} as the right position for number 6 (6 should at i=6 and j=6 in the grid). It works perfectly in Debug Configuration, but with Distrib Conf, it seems like I have problem with the l and m vars -> the loop breaks well at l=6 and m=6 (hence, isPossibleValue function seems to work well), but then my cell variable is equal to {1;0}, instead of {6;6} !
I am using this kind of process (returning a cell[2]) with many other functions, and I don't have any problem with the Distrib Conf.
My second problem, - and this one is provoking the app to crash - is with the following code (again, perfectly working with debug configuration).
- (void)solver:(int[9][9])aMatrix {
if (!Termine) {
int *emptyCell = [self firstEmptyCell:aMatrix];
if (emptyCell != nil) {
int i = emptyCell[0];
int j = emptyCell[1];
for (int k = 1; k <= 9; k++) {
if ([self isPossibleValue:aMatrix value:k line:i column:j]) {
aMatrix[i][j] = k;
[self solver:aMatrix];
aMatrix[i][j] = 0;
}
}
}
else {
if (!Termine) {
Termine = TRUE;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
res[i][j] = aMatrix[i][j];
}
}
}
}
Note : one solution, - I am not sure that's the best one - is to change optimization level from "fastest, smallest" to "none".
Thanks for your help !
For the first case the problem is in int cell[2];
. It has a local scope, and after you return from the function the memory zone pointed by this array very likely will be reallocated for other needs. A quick solution is to add static
specifier:
static int cell[2];
For the second snippet, make sure that emptyCell
each time assigns to i
and j
values which doesn't exceed aMatrix
bounds [0..8][0..8].
精彩评论