Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

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?

like image 227
Gerharddc Avatar asked Jun 24 '13 15:06

Gerharddc


People also ask

How do I fix debug assertion failed?

You may uninstall Microsoft Visual C++ Runtime Package from Program & Features then reinstall it again. Thereafter, check if the issue persists. You may run system file checker [SFC] scan on the computer which will replace the missing or corrupt files & check if the issue persists.

What is debug assertion failed Visual C++?

An assertion statement specifies a condition that you expect to hold true at some particular point in your program. If that condition does not hold true, the assertion fails, execution of your program is interrupted, and this dialog box appears. Debug the assertion or get help on asserts.

What is Microsoft Visual C++ runtime library error?

Your graphics card driver can often cause Microsoft Visual C++ runtime error, and to fix it you need to reinstall your graphics card driver. If you do not then it means that you need to update your graphics card driver to the latest version that is compatible with your Windows 10 system version.

What is debug assert?

Assert(Boolean, Debug+AssertInterpolatedStringHandler) Checks for a condition; if the condition is false , outputs a specified message and displays a message box that shows the call stack.


2 Answers

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

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

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.

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

like image 83
Chemistpp Avatar answered Oct 23 '22 23:10

Chemistpp


The debugger cannot predict the future, but you can tell it to break on all exceptions, (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.

like image 23
doctorlove Avatar answered Oct 23 '22 22:10

doctorlove