title: How do I debug or fix the endless loop and heap corruption issue involving boost::interprocess managed_shared_memory?
body: <p>I have the following "first-chance exception" message which is coming from a DLL I wrote which is running inside an executable that I did not write. That is, the DLL is a plugin. The first time this exception fires, an attempt to open a shared memory map file is failing.  If I ignore first chance exceptions and just run, the application freezes or crashes eventually.</p>
  
  <pre><code>First-chance exception at 0x76a7c41f in notmyexe.exe: Microsoft C++ exception: boost::interprocess::interprocess_exception at memory location 0x002bc644..
  </code></pre>
  
  <p>After several hours it appears to be caused by a block of code which is looping endlessly until an expected exception condition clears. It turns out that if it never does clear, and then, eventually, this exception turns into another low-level-exception-condition and/or turns into heap corruption.  All of this is just in an effort to open a shared memory area using Boost::interprocess.</p>
  
  <p>The first thing that complicates things is that on my Visual C++ 2008 based project, the first <code>boost::interprocess::interprocess_exception</code> first-chance exception is not thrown and identified as the location where it came from because the Visual C++ 2008 compiler cannot find the complex boost-flavor templates code in question. However by single stepping through the assembly language view, I found the code that blows up.</p>
  
  <p>The top level line of my own code that it all starts to go bad on is:</p>
  
  <pre><code>  segment = new managed_shared_memory(   open_or_create
                                        ,  MEMORY_AREA_NAME
                                        , SHARED_AREA_SIZE );          
  </code></pre>
  
  <p>The above <code>managed_shared_memory</code> class is from interprocess_fwd.hpp, and is a standard part of the boost shared memory API/headers.  Because it's template based, the above expands into about a 2Kchars long C++ boost template expression, which is truncated at different lengths by the linker, and by the debugger.  Visual C++ 2008 has no more source code debugging capabilities, it seems when these limits are in play.</p>
  
  <p>For example, when it blows up I get this call stack:</p>
  
  <pre><code>    KernelBase.dll!76a7c41f()   
      [Frames below may be incorrect and/or missing, no symbols loaded for KernelBase.dll]    
      KernelBase.dll!76a7c41f()   
  &gt;   msvcr90d.dll!_malloc_dbg(unsigned int nSize=2290875461, int nBlockUse=264, const char * szFileName=0x01fcb983, int nLine=1962999808)  Line 160 + 0x1b bytes C++
      8bfc4d89()  
  </code></pre>
  
  <p>No actual end-user written source functions appear in the stack dump above.</p>
  
  <p>How should I debug this? Secondly, is there some known problem with boost-interprocess, with Visual C++ 2008?  Third, what is the boost code below doing and why must it endlessly loop? </p>
  
  <pre><code>boost::interprocess::basic_managed_shared_memory&lt;char,
     boost::interprocess::rbtree_best_fit&lt;boost::interprocess::mutex_family,
          boost::interprocess::offset_ptr&lt;void,int,unsigned int,0&gt;,0&gt;,
          boost::interprocess::iset_index&gt;::basic_managed_shared_memory&lt;char,boo...
  </code></pre>
  
  <p>Further layers down, we get to:</p>
  
  <pre><code>basic_managed_shared_memory (open_or_create_t,
                                const char *name, size_type size,
                                const void *addr = 0, const permissions&amp; perm = permissions())
        : base_t()
        , base2_t(open_or_create, name, size, read_write, addr,
                  create_open_func_t(get_this_pointer(),
                  ipcdetail::DoOpenOrCreate), perm)
     {}  
  </code></pre>
  
  <p>Anyways, don't try to debug this at home kids, here's what happens:</p>
  
  <p><img src="https://i.stack.imgur.com/VUXrO.png" alt="enter image description here"></p>
  
  <p>Finally, using my ninja-like ability to single step through several million lines of assembly language I have defeated Visual C++ 2008's evil debugger limitations, and have found the code in question.  </p>
  
  <p>This is what is blowing up in fact: <code>create_device&lt;FileBased&gt;(dev...</code>.</p>
  
  <p>Some context here:
  managed_open_or_create_impl.h line 351...</p>
  
  <pre><code>else if(type == DoOpenOrCreate){
           //This loop is very ugly, but brute force is sometimes better
           //than diplomacy. If someone knows how to open or create a
           //file and know if we have really created it or just open it
           //drop me a e-mail!
           bool completed = false;
           while(!completed){
              try{
                 create_device&lt;FileBased&gt;(dev, id, size, perm, file_like_t()); // &lt;-- KABOOM!
                 created     = true;
                 completed   = true;
              }
              catch(interprocess_exception &amp;ex){
                 if(ex.get_error_code() != already_exists_error){
                    throw;
                 }
                 else{
                    try{
                       DeviceAbstraction tmp(open_only, id, read_write);
                       dev.swap(tmp);
                       created     = false;
                       completed   = true;
                    }
                    catch(interprocess_exception &amp;e){
                       if(e.get_error_code() != not_found_error){
                          throw;
                       }
                    }
                    catch(...){
                       throw;
                    }
                 }
              }
              catch(...){
                 throw;
              }
              thread_yield();
           }
        }
  </code></pre>