Visual C++ 查找导致“调试断言失败”的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17279187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:04:09  来源:igfitidea点击:

Visual C++ find line causing "Debug Assertion failed"

c++visual-c++gccvectorg++

提问by Gerharddc

I am trying to get a C++ program that works fine when compiled with gcc to work properly on Visual C++. My problem is that I am now getting the following error:

我正在尝试获得一个 C++ 程序,该程序在使用 gcc 编译时可以正常工作,以便在 Visual C++ 上正常工作。我的问题是我现在收到以下错误:

Debug Assertion Failed!

Program: C:\WINDOWS\SYSTEM32\MSVCP110D.dll
File: c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector
Line: 1140

Expression: vector subscript out of range

My real problem is that I do not know when or where this happens. By pressing break in the error window I am merely taken to the part of the vector class where the exception ultimately happened. I want to find the place in my application which actually caused it. I have managed to narrow it down to this block of code:

我真正的问题是我不知道这种情况何时何地发生。通过在错误窗口中按下中断,我只是被带到了最终发生异常的向量类的部分。我想在我的应用程序中找到实际导致它的地方。我设法将其缩小到此代码块:

    for(unsigned int i=0;i<openPolygonList.size();i++)//error somewhere in here
    {
        if (openPolygonList[i].size() < 1) continue;
        for(unsigned int j=0;j<openPolygonList.size();j++)
        {
            if (openPolygonList[j].size() < 1) continue;

            Point diff = openPolygonList[i][openPolygonList[i].size()-1] - openPolygonList[j][0];
            int64_t distSquared = vSize2(diff);

            if (distSquared < 2 * 2)
            {
                if (i == j)
                {
                    polygonList.push_back(openPolygonList[i]);
                    openPolygonList.erase(openPolygonList.begin() + i);
                }else{
                    for(unsigned int n=0; n<openPolygonList[j].size(); n++)
                        openPolygonList[i].push_back(openPolygonList[j][n]);

                    openPolygonList[j].clear();
                }
            }
        }
    }

Simply placing breakpoints at each line where a vector is used is not an option because the loop iterates thousands of times and having to press continue each time will literally take me hours. Is there any way I can tell the debugger to brake on the appropriate line once the error occurs. It can help me to inspect the variables and to determine which variable is out of range?

简单地在使用向量的每一行放置断点不是一种选择,因为循环迭代数千次并且每次都必须按继续键实际上需要我几个小时。一旦发生错误,我有什么方法可以告诉调试器在适当的线路上刹车。它可以帮助我检查变量并确定哪个变量超出范围?

采纳答案by Chemistpp

I think the problem is that you are erasing members of the vector you are iterating through. What happens if you erase the first element?

我认为问题在于您正在擦除您正在迭代的向量的成员。如果删除第一个元素会发生什么?

i 1 2 3 Ei v1 v2 v3

i 1 2 3 Ei v1 v2 v3

If we erase 1 when i = 1, our vector indices and values are below and now i = 2.

如果我们在 i = 1 时擦除 1,我们的向量索引和值低于现在 i = 2。

i 1 2 Ei v2 v3

i 1 2 Ei v2 v3

Ultimately, I think you can iterate past the end of the vector causing you to have a pointer that points past the end of the vector. Complete guess work here, but there is probably an easier way to do what you're trying to do. I just can't figure out what you're trying to do.

最终,我认为您可以遍历向量的末尾,从而使指针指向向量的末尾。在这里完全猜测工作,但可能有一种更简单的方法来做你想做的事情。我只是想不通你想做什么。

It looks like you're trying to invert the rows and columns of a two dimensional array while storing the diagonal polygons int he array into a new array. Anyway, yes, but a red circle at the beginning of the for loop and walk through your code line by line.

看起来您正在尝试反转二维数组的行和列,同时将数组中的对角多边形存储到新数组中。无论如何,是的,但是在 for 循环开始处有一个红色圆圈并逐行浏览您的代码。

I would create temp vectors and then modify those ones in the for loop and then replace the vector openPolygonList.

我会创建临时向量,然后在 for 循环中修改这些向量,然后替换向量 openPolygonList。

回答by doctorlove

The debugger cannot predict the future, but you can tell it to break on allexceptions, (ctrl+atl+e and tick all the boxes under "Thrown"). When the assert happens walk down the call stack to your code it will tell you which line is causing the problem.

调试器无法预测未来,但您可以告诉它在所有异常时中断(ctrl+atl+e 并勾选“Thrown”下的所有框)。当断言发生时,沿着调用堆栈向下走到您的代码,它会告诉您哪一行导致了问题。

回答by kovarex

You can open the call stack window (Debug->windows->CallStack) and find the spot in your program that led to the assertion. It should be 2 or 3 lines below the top line.

您可以打开调用堆栈窗口 (Debug->windows->CallStack) 并找到程序中导致断言的位置。它应该比顶线低 2 或 3 行。