title: Dwarf Error: Cannot find DIE
body: <p>I am having a lot of trouble debugging a segmentation fault in a C++ project in XCode 4.</p>
  
  <p>I only get a segfault when I built with the "LLVM 2.0" compiler option and use -O3 optimization. From what I understand, there are limited debugging options when one is using optimization, but here is the debug output I get after I run in Xcode with gdb turned on:</p>
  
  <pre><code>warning: Got an error handling event: "Dwarf Error: Cannot find DIE at 0x3be2 referenced from DIE at 0x11d [in module /Users/imran/Library/Developer/Xcode/DerivedData/cgo-hczcifktgscxjigfphieegbpxxsq/Build/Products/Debug/cgo]".
  No memory available to program now: unsafe to call malloc
  </code></pre>
  
  <p>I can't get gdb to give me any useful info after that (like a trace), but I'm not sure I really know how to use it properly. When I try to use the "LLDB" debugger Xcode just crashes (which has been a common theme since I started using it).</p>
  
  <p>My program is deterministic, but when I try to isolate the problem with print statements the behavior will change. For example if I add <code>cout &lt;&lt; "hello";</code> at one point the segfault goes away. Other print statements cause my program to segfault in a different iteration of its main loop. And naturally when I put in enough print statements to supposedly pinpoint the offending code, the segfault seems to occur after one line but before the next (i.e. nowhere).</p>
  
  <p>I am using pointers and dynamic memory allocation, which is likely the cause of the problem, but since I can't narrow down the block of code causing the error I don't know what code to show here.</p>
  
  <p>I tried profiling with the "Leaks" tool in Instruments, but it didn't find any leaks.</p>
  
  <p>Any advice? I am very inexperienced with debugging so anything would help, really.</p>
  
  <p>EDIT: Solved. Given certain inputs, my program would try to read past the end of an array.</p>