Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
I created a memory corruption by the following statement:
    char *msg = "Default message";
    free(msg);
 
This causes the memory corruption exception as:
Break instruction exception - code 80000003 (first chance)
 
How to detect and debug memory corruption issues in Windows probably using Windbg? What all are the windbg commands needed?
Posted 5-May-11 10:23am
Edited 5-May-11 10:28am
v2
Comments
SAKryukov at 5-May-11 15:29pm
   
I hope you understand why you code corrupts the memory? Or you need some explanation?
--SA
rupeshkp728 at 5-May-11 15:39pm
   
Thanks SAKryokov for the reply.
I know what is corruption of memory.
I wanted a way to debug it in case of large code base.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

If you have a application which is already working & with many lines of code ,
I would suggest you to use Application Verifier.
Also Rational purifier can come handy.
No need to mention that they wont guarantee 100% tracking , still might be useful in your case.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Why not just run your app under the debugger in Visual Studio? When an exception occurs, the debugger will point to the line that faulted.
 
If you really want to use windbg, see here: http://windbg.info/doc/1-common-cmds.html[^]
  Permalink  
v2
Comments
SAKryukov at 5-May-11 15:30pm
   
It usually helps, my 5 :-)
--SA
Hans Dietrich at 5-May-11 15:33pm
   
Thanks. I used to use windbg a lot, but the VS debugger has improved quite a bit over the years (unlike the IDE :) ).
SAKryukov at 5-May-11 17:19pm
   
IDE became better as well, to my taste. I like VS 2008 even though I have 2010, too. I remember how I hated v.6.
--SA
rupeshkp728 at 5-May-11 15:38pm
   
Thanks Hans for the reply.
I know what is corruption of memory.
Whatever you have said I have already performed it using windbg.
I wanted a way to debug it in case of large code base.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

There are two ways the way I see it. Run all your code through a debugger. Getting 100% code coverage is close to impossible, unless your code is fully test-driven. The other way is to set up code review with a couple of experienced programmers. My experience is that you can find error patterns, and identify not only problematic code, but also problematic programmers, guiding you to the hot spots that needs validation.
Edit: If you have a lot of code to go through, getting a static code anlysis software might be of great help.
  Permalink  
v2
Comments
rupeshkp728 at 5-May-11 15:49pm
   
thanks niklas for the inputs
SAKryukov at 5-May-11 17:21pm
   
I agree with that; good notes about test-driven development and static analysis. My 5.
--SA
Niklas Lindquist at 6-May-11 2:33am
   
Thanks
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

There is no real panacea for finding memory corruptions, your best bet is to try to avoid 'raw' pointers
 
- Use stl containers to hold lists (vector, list etc)
- Wrap pointer use inside a class, and have check code all thru it, at least in debug (CString etc)
- Code reviews and test units
 
The debug libraries will do some work to find you exceeding your memory block, but they won't catch them all
 
Interestingly, the example you've quoted is not a memory corruption, it's you free'ing memory that hasn't been allocated
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

There are a couple of tools that track down code which is problematic with regard to memory corruption or similar, both at compile time, and at run time. I am using a commercial one which is quite good, but I've found it slows down both compilation and run time behaviour by an order of magnitude at least.
 
There are a few articles on this topic as well, try out any of these links:
Memory Leak detection in .NET
Windows Memory leak detection
Visual Leak Detector
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You could also start windbg and look at the help file that comes with it. There is a lot of good information in there.
 
Or from a command prompt in the directory containing windbg, just type windbg /?
 
Hope that helps.
  Permalink  
Comments
rupeshkp728 at 5-May-11 15:36pm
   
thanks for the reply

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 384
1 Maciej Los 290
2 Sergey Alexandrovich Kryukov 230
3 CPallini 170
4 Shweta N Mishra 165
0 OriginalGriff 7,760
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,785


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 6 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100