Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 861
lezebulon Avatar asked Oct 30 '12 15:10

lezebulon


2 Answers

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.
like image 59
David Hammen Avatar answered Sep 30 '22 17:09

David Hammen


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).

like image 41
Luchian Grigore Avatar answered Sep 30 '22 17:09

Luchian Grigore