title: How to debug confusingly big code?
body: <p>I am not a c++ programmer but try to debug some complex code. Not the best preconditions, I know...</p>
  
  <p>So I have an openfoam solver which uses (includes) lots of code and I am struggling to really find the error. I compile with</p>
  
  <blockquote>
    <p>SOURCE=mySolver.C ;  g++ -m64 -Dlinux64 -DWM_DP -Wall -Wextra -Wno-unused-parameter -Wold-style-cast -O3  -DNoRepository -ftemplate-depth-100 -I/opt/software/openfoam/OpenFOAM-2.0.5/src/dynamicMesh/lnInclude {more linking} -I.  -fPIC -c $SOURCE -o Make/linux64Gcc46DPOpt/mySolver.o</p>
  </blockquote>
  
  <p>and after running the solver with the appropriate options, it crashes at the end after (or while) my return statement:</p>
  
  <pre><code>BEFORE return 0
  
  *** glibc detected *** /opt/software/openfoam/myLibs/applications/bin/linux64Gcc46DPOpt/mySolver: double free or corruption (!prev): 0x000000000d3b7c30 ***
  ======= Backtrace: =========
  /lib64/libc.so.6[0x31c307230f]
  /lib64/libc.so.6(cfree+0x4b)[0x31c307276b]
  /opt/software/openfoam/ThirdParty-2.0.5/platforms/linux64/gcc-4.5.3/lib64/libstdc++.so.6(_ZNSsD1Ev+0x39)[0x2b34781ffff9]
  /opt/software/openfoam/myLibs/applications/bin/linux64Gcc46DPOpt/mySolver(_ZN4Foam6stringD1Ev+0x18)[0x441e2e]
  /opt/software/openfoam/myLibs/applications/bin/linux64Gcc46DPOpt/mySolver(_ZN4Foam4wordD2Ev+0x18)[0x442216]
  /lib64/libc.so.6(__cxa_finalize+0x8e)[0x31c303368e]
  /opt/software/openfoam/myLibs/lib/linux64Gcc46DPOpt/libTMP.so[0x2b347a17f866]
  ======= Memory map: ========
  ...
  </code></pre>
  
  <p>My solver looks like (sorry, I can't post all parts):</p>
  
  <pre><code>#include "stuff1.H"
  #include "stuff2.H"
  
  int main(int argc, char *argv[])
  {
  #include "stuff3.H"
  #include "stuffn.H"
  
      while (runTime.run())
      {
  
          ...
  
      }
  
  Info&lt;&lt; "BEFORE return 0\n" &lt;&lt; endl;
  
  return(0);
  }
  </code></pre>
  
  <p>Running the solver with gdb with <a href="https://stackoverflow.com/a/2903152/2622293">setting</a> <code>set environment MALLOC_CHECK_ 2</code> yields to:</p>
  
  <pre><code>BEFORE return 0
  
  Program received signal SIGABRT, Aborted.
  0x00000031c3030265 in raise () from /lib64/libc.so.6
  (gdb) bt
  #0  0x00000031c3030265 in raise () from /lib64/libc.so.6
  #1  0x00000031c3031d10 in abort () from /lib64/libc.so.6
  #2  0x00000031c3075ebc in free_check () from /lib64/libc.so.6
  #3  0x00000031c30727f1 in free () from /lib64/libc.so.6
  #4  0x00002aaab0496ff9 in std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::~basic_string() ()
     from /opt/software/openfoam/ThirdParty-2.0.5/platforms/linux64/gcc-4.5.3/lib64/libstdc++.so.6
  #5  0x0000000000441e2e in Foam::string::~string (this=0x2aaaac0bd3c8, __in_chrg=&lt;value optimized out&gt;) at /opt/software/openfoam/OpenFOAM-2.0.5/src/OpenFOAM/lnInclude/string.H:78
  #6  0x0000000000442216 in Foam::word::~word (this=0x2aaaac0bd3c8, __in_chrg=&lt;value optimized out&gt;) at /opt/software/openfoam/OpenFOAM-2.0.5/src/OpenFOAM/lnInclude/word.H:63
  #7  0x00000031c303368e in __cxa_finalize () from /lib64/libc.so.6
  #8  0x00002aaab2416866 in __do_global_dtors_aux () from /opt/software/openfoam/myLibs/lib/linux64Gcc46DPOpt/libTMP.so
  #9  0x0000000000000000 in ?? ()
  (gdb) 
  </code></pre>
  
  <p>How should I proceed to find the real source of my error?</p>
  
  <p>Btw. I saw <a href="http://www.cplusplus.com/forum/unices/73830/" rel="nofollow noreferrer">this</a> and <a href="https://stackoverflow.com/questions/18267766/c-application-crash">this</a> which is similar but not solving my issue. Also valgrind isn't working correctly for me. I know it has to do with some wrong (de-)allocation but I don't know how to really find the problem.</p>
  
  <p><strong>/Edit</strong></p>
  
  <p>I wasn't able to locate my problem yet...</p>
  
  <p>I think the backtrace which I posted above (position #8) shows the problem is in the code which compiles to <code>libTMP.so</code>. In the <em>Make/options</em> file I added the option <strong>-DFULLDEBUG -g -O0</strong>. I thought it's possible to track the bug then but I don't know how.</p>
  
  <p>Any help is highly appreciated!</p>