Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 5-May-11 9:28am
v2
Comments
Sergey Alexandrovich Kryukov 5-May-11 15:29pm    
I hope you understand why you code corrupts the memory? Or you need some explanation?
--SA
rupeshkp728 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.

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.
 
Share this answer
 
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[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 5-May-11 15:30pm    
It usually helps, my 5 :-)
--SA
Hans Dietrich 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 :) ).
Sergey Alexandrovich Kryukov 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 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.
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.
 
Share this answer
 
v2
Comments
rupeshkp728 5-May-11 15:49pm    
thanks niklas for the inputs
Sergey Alexandrovich Kryukov 5-May-11 17:21pm    
I agree with that; good notes about test-driven development and static analysis. My 5.
--SA
Niklas L 6-May-11 2:33am    
Thanks
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
 
Share this answer
 
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
 
Share this answer
 
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.
 
Share this answer
 
Comments
rupeshkp728 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900