IOS:分发(发布)配置错误和崩溃

发布于 2024-10-13 15:44:43 字数 2334 浏览 4 评论 0原文

我创建了一个应用程序来解决数独网格......但是 这是我的问题:我的应用程序有一些错误,甚至在我的分发配置编译时崩溃(相当于发布配置)。

下面是它出现错误的代码:

- (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=6j= 6 在网格中)。它在调试配置中完美运行,但使用 Distrib Conf 时,似乎我对 lm 变量有问题 ->循环在 l=6m=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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷情妓 2024-10-20 15:44:43

对于第一种情况,问题出在 int cell[2]; 中。它具有本地作用域,从函数返回后,该数组指向的内存区域很可能会被重新分配以满足其他需求。一个快速的解决方案是添加 static 说明符:

static int cell[2];

对于第二个片段,确保 emptyCell 每次分配给 ij< /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 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].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文