title: How to hunt a Heisenbug
body: <p>Recently, we received a bug report from one of our users: something on the screen was displayed incorrectly in our software. Somehow, we could not reproduce this in our development environment (Delphi 2007).</p>
  
  <p>After some further study, it appears that this bug <em>only</em> manifests itself when "Code optimization" is turned <em>on</em>.</p>
  
  <p>Are there any people here with experience in hunting down such a <a href="http://en.wikipedia.org/wiki/Unusual_software_bug#Heisenbug" rel="nofollow noreferrer">Heisenbug</a>? Any specific constructs or coding bugs that commonly cause such an issue in Delphi software? Any places you would start looking?</p>
  
  <p>I'll also just start debugging the whole thing in the usual way, but any tips specific to Optimization-related bugs (*) would be more than welcome!</p>
  
  <p>(*) Note: I don't mean to say that the bug is <em>caused</em> by the optimizer; I think it's much more likely some wonky construct in the code is somehow pushed "over the edge" by the optimizer.</p>
  
  <h2>Update</h2>
  
  <p>It seems the bug boils down to a record being fully initialized with zeros when there's no code optimization, and the same record containing some random data when there <em>is</em> optimization. In this case, the random data seems to cause an enum type to contain invalid data (to my great surprise!).</p>
  
  <h2>Solution</h2>
  
  <p>The solution turned out to involve an unitialized local record variable somewhere deep in the code. Apparently, without optimization the record <em>was</em> reset (heap?), and with optimization turned <em>on</em>, the record was filled with the usual garbage. Thanks to you all for your contributions --- I learned a lot along the way!</p>