General way of solving Error: Stack around the variable 'x' was corrupted

66,505

Solution 1

Is there a general way to debug this error with VS2010?

No, there isn't. What you have done is to somehow invoke undefined behavior. The reason these behaviors are undefined is that the general case is very hard to detect/diagnose. Sometimes it is provably impossible to do so.

There are however, a somewhat smallish number of things that typically cause your problem:

  • Improper handling of memory:
    • Deleting something twice,
    • Using the wrong type of deletion (free for something allocated with new, etc.),
    • Accessing something after it's memory has been deleted.
  • Returning a pointer or reference to a local.
  • Reading or writing past the end of an array.

Solution 2

This can be caused by several issues, that are generally hard to see:

  • double deletes
  • delete a variable allocated with new[] or delete[] a variable allocated with new
  • delete something allocated with malloc
  • delete an automatic storage variable
  • returning a local by reference

If it's not immediately clear, I'd get my hands on a memory debugger (I can think of Rational Purify for windows).

Solution 3

This message can also be due to an array bounds violation. Make sure that your function (and every function it calls, especially member functions for stack-based objects) is obeying the bounds of any arrays that may be used.

Solution 4

Actually what you see is quite informative, you should check in near x variable location for any activity that might cause this error.

Below is how you can reproduce such exception:

int main() {
    char buffer1[10];
    char buffer2[20];
    memset(buffer1, 0, sizeof(buffer1) + 1);
    return 0;
}

will generate (VS2010):

Run-Time Check Failure #2 - Stack around the variable 'buffer1' was corrupted.

obviously memset has written 1 char more than it should. VS with option \GS allows to detect such buffer overflows (which you have enabled), for more on that read here: http://msdn.microsoft.com/en-us/library/Aa290051.

You can for example use debuger and step throught you code, each time watch at contents of your variable, how they change. You can also try luck with data breakpoints, you set breakpoint when some memory location changes and debugger stops at that moment,possibly showing you callstack where problem is located. But this actually might not work with \GS flag.

For detecting heap overflows you can use gflags tool.

Solution 5

I was puzzled by this error for hours, I know the possible causes, and they are already mentioned in the previous answers, but I don't allocate memory, don't access array elements, don't return pointers to local variables...

Then finally found the source of the problem:

*x++;

The intent was to increment the pointed value. But due to the precedence ++ comes first, moving the x pointer forward then * does nothing, then writing to *x will be corrupt the stack canary if the parameter comes from the stack, making VS complain.

Changing it to (*x)++ solves the problem.

Hope this helps.

Share:
66,505
lezebulon
Author by

lezebulon

Updated on July 10, 2022

Comments

  • lezebulon
    lezebulon almost 2 years

    I have a program which prompts me the error in VS2010, in debug :

    Error: Stack around the variable 'x' was corrupted
    

    This gives me the function where a stack overflow likely occurs, but I can't visually see where the problem is.

    Is there a general way to debug this error with VS2010? Would it be possible to indentify which write operation is overwritting the incorrect stack memory? thanks