IOS:分发(发布)配置错误和崩溃
我创建了一个应用程序来解决数独网格......但是 这是我的问题:我的应用程序有一些错误,甚至在我的分发配置编译时崩溃(相当于发布配置)。
下面是它出现错误的代码:
- (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;
}
我有一个特殊的数独网格,我应该在其中找到 {6,6} 作为数字 6 的正确位置(6 应该位于 i=6 和 j= 6 在网格中)。它在调试配置中完美运行,但使用 Distrib Conf 时,似乎我对 l 和 m 变量有问题 ->循环在 l=6 和 m=6 处很好地中断(因此,isPossibleValue 函数似乎运行良好),但是我的 cell 变量等于 {1;0},而不是 {6;6}!
我正在将这种过程(返回cell[2])与许多其他函数一起使用,并且我对 Distrib Conf 没有任何问题。
我的第二个问题 - 这个问题导致应用程序崩溃 - 是使用以下代码(同样,与调试配置完美配合)。
- (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];
}
}
}
}
注意:一种解决方案 - 我不确定这是最好的解决方案 - 是将优化级别从“最快,最小”更改为“无”。
感谢您的帮助 !
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 !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于第一种情况,问题出在
int cell[2];
中。它具有本地作用域,从函数返回后,该数组指向的内存区域很可能会被重新分配以满足其他需求。一个快速的解决方案是添加static
说明符:对于第二个片段,确保
emptyCell
每次分配给i
和j< /code> 值不超过
aMatrix
边界 [0..8][0..8]。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 addstatic
specifier:For the second snippet, make sure that
emptyCell
each time assigns toi
andj
values which doesn't exceedaMatrix
bounds [0..8][0..8].