views:

108

answers:

3

This is a further question for my previous one here.

Is there a general solution for this?

+8  A: 
  • Fix all dangling pointers
  • Fix all buffer overflows
  • Use pointers only where they are really needed
swegi
Can you provide an exact solution for my previous problem?
ollydbg
@ollydbg: You've asked for a "general solution", but now you want an "exact solution"?
Oli Charlesworth
+1  A: 

Reading your original post I'm not 100% you are facing a heap-corruption and you really hope you don't because if you would this is one of the most tricky errors to track down and AFAIK there are no solutions that always work.

However, if you are positive its a heap-corruption and you are on the windows platform you could try out the tool gflags. This advanced debugging tools allow you to install a debug heap manager in order to make it possible to find certain kinds of heap corruptions. One of the neat options it has is to make each allocation in it's own page or to write protect the heap datastructures. This makes it easier to pinpoint the exact location of the heap-corruption. It sucks lots of memory and CPU though.

Another tip if you are using Visual Studio is that if you manage to find that something is corrupting the data after a certain point of time and you wish to find the code that corrupts the data you can use Data Breakpoint to break whenever someone changes the data in question.

I wish you good luck.

FuleSnabel
+1  A: 

Adding to swegi's answer I would check all your C++ constructors and all C initializations. Transform all dynamic C++ constructors (those where you put the init code in the function body) into static ones (where you initialize with the special constructor syntax). And be sure that you init all pointers and pointer members to 0. For C, I would initialize all variables.

Then, on unix I would use valgrind, usually this is quite good in finding all access violations and if you compile with all debugging options on it is able to trace it to the source line. There should be something similar on windows, too.

Jens Gustedt
I very much wonder what equivalent is available. I have never seen one in my meagre ~3 years of Windows development experience.
Matt Joiner