title: Freeing memory allocated in a different DLL
body: <p>I have an EXE file using a DLL file which is using another DLL file. This situation has arisen:</p>
  
  <p>In DLL file 1:</p>
  
  <pre><code>class abc
  {
      static bool FindSubFolders(const std::string &amp; sFolderToCheck, 
                                 std::vector&lt; std::string &gt; &amp; vecSubFoldersFound);
  }
  </code></pre>
  
  <p>In DLL file 2:</p>
  
  <pre><code>void aFunction()
  {
      std::vector&lt;std::string&gt; folders;
      std::string sLocation;
      ...
      abc::FindSubFolders(sLocation, folders)
  }
  </code></pre>
  
  <p>In release mode, everything works fine. But in debug mode, I come up with an assertion failure in the destructor of one of the <code>std::strings</code> in the folders vector (when folders goes out of scope at the end of aFunction):</p>
  
  <p><code>dbgheap.c : line 1274</code></p>
  
  <pre><code>/*
   * If this ASSERT fails, a bad pointer has been passed in. It may be
   * totally bogus, or it may have been allocated from another heap.
   * The pointer MUST come from the 'local' heap.
   */
  _ASSERTE(_CrtIsValidHeapPointer(pUserData));
  </code></pre>
  
  <p>I assume this is because the memory has been allocated on DLL file 1's heap, but is being freed in DLL file 2.</p>
  
  <p>The comment in <code>dbgheap.c</code> seems pretty insistent that this is a problem. </p>
  
  <p>Why is this such a problem, when it seems to work fine if I just ignore it? Is there a non-assertion-failing way of doing this?</p>