Have you ever seen a bug that disappears when you try to debug it? This situation has a name: heisenbug[1]. A special type of heisenbug happened to me at least twice and recently to a colleague of mine.

Different Behaviors

These were the symptoms: the program (compiled with gcc -O2) always crashed when executing a particular function. Once removed the optimization flag, everything seemed to work fine.

Probably the reason is that the process allocates the memory differently when it's compiled with and without optimizations. This is understandable but it has some drawbacks. In fact, in this particular case, the bug has emerged because of the optimization. However in other cases, bugs may just be hidden by the work done by the optimizer.

Bottom Line

It's not always possible to do extensive tests on two differently compiled versions of the same application. Fortunately you can detect this kind of bugs with memory analysis tools, like Valgrind.



Werner Heisenberg image by Bundesarchiv, Bild 183-R57262 taken from Wikimedia Commons licensed under the Creative Commons Attribution-Share Alike 3.0 Germany license.


  1. Heisenbug: see point 8 in this old Coding Horror post. A more exhaustive description is given in this Wikipedia page. ↩︎