Jkh2
Jkh2

Reputation: 1030

Debugging problems with Visual Studio 2010 C++ - Odd values showing up

I am encountering some really odd behavior with my code.

It is throwing an access violation error when I run it and through the debugger I managed to find out the line which is causing it:

for( int x=0; x<width; x++) {

    int current = edgeKernelXY(left+x,top+y,true,0);
    ....
}

I placed a debug point in the method edgeKernelXY and the code never even got into the method.

The next thing I checked was what the values that I am passing into it are. Left, top and y seem normal enough. However according to the debugger, x = 19840810 and current = 19840810. I don't understand how this could have happened especially if I declared x to be 0 at the beginning of the loop. Width is correct at 40.

x and current have not been declared anywhere else in scope of the forloop. What could be going wrong here?

EDIT:

I changed the code as follows:

    for( int x=0; x<width; x++) {
        int current = edgeKernelXY(left,top+y,true,0);
        if( current > THRESHOLD && 
            edgeKernelXY(left+x,top+y,true,1) > THRESHOLD &&
            edgeKernelXY(left+x,top+y,true,2) > THRESHOLD ) {
        } else {
            current = 0.0f;
        }

Specifically I changed left+x on the first call of edgeKernelXY to just left. This seems to run and the second call to edgeKernelXY shows x set correctly to 0. However the behavior is not what I want. Left + x still gets me crazy values for x which is causing an access violation.

    for( int x=0; x<width; x++) {

        int current;
        current = 0;
        current = edgeKernelXY(left+x,top+y,true,0);

Also shows problems with current.

Upvotes: 2

Views: 672

Answers (2)

Michael Burr
Michael Burr

Reputation: 340218

Since you're debugging a release build you can't depend on the debugger showing the right values in watch windows, etc. Debugging release mode can be done, but is considerably trickier. Generally it requires looking at the disassembly to understand what's going on and what the state of things is (ie., x is almost certainly being kept in a register).

If you can't perform a full debug release, you can still get a much better debugging experience by building with optimizations turned off (which can be done in release builds and can be done without linking to debug libraries).

Assuming that you're building in the IDE, just go into the project settings and change the C/C++ - Optimization - Optimization value to Disabled (/Od).

If this causes a problem by setting it project-wide (which it shouldn't), you can do the same on a per-source-file basis by right clicking on the source files you're interested in debugging and setting the option in the properties for that source file. Just remember to clear those settings when you're done (and don't check them into source control) because the IDE doesn't make it obvious that there are source file specific settings so it can be confusing down the road.

Upvotes: 3

Gangadhar
Gangadhar

Reputation: 1903

Few things to check

  1. Are there any double deletes in your program which might be messing up the heap (I know it doesn't explain the issue of the x on the stack, but something to check nevertheless)
  2. Are there any statements between the for starting the function call edgeKernelXY? If so, please check those for any potential stack-overflows
  3. The value of x being wrong smells like stack corruption to me which can happen due to multiple reasons
    • Is there an array that you created at the beginning of the function which overran the stack?
    • Could there be a function call that returned a pointer to an array (on its stack) which you are using assuming that the data is already available

All of the above will not give you the exact answer you are looking for, but some options for you to check. And most importantly - have you run your program through some memory profiler (like Valgrind) and checked the output? More often than not, that will show the cause of this problem.

If you do find out the cause for the problem, please do post the approach you followed. It will make the community's experience richer. Thanks.

Upvotes: 0

Related Questions