title: Heap corruption detected error when attempting to free pointer
body: <p>I'm pretty new to C++ and have run into a problem which I have not been able to solve. I'm trying to convert a System::String to a wchar_t pointer that I can keep for longer than the scope of the function. Once I'm finished with it, I want to clean it up properly. Here is my code:</p>
  
  <pre><code>static wchar_t* g_msg;
  
  void TestConvert()
  {
      pin_ptr&lt;const wchar_t&gt; wchptr = PtrToStringChars("Test");
      g_msg = (wchar_t*)realloc(g_msg, wcslen(wchptr) + 1);
      wcscpy(g_msg, wchptr);
  
      free (g_msg); // Will be called from a different method
  }
  </code></pre>
  
  <p>When the free is called, I'm getting "HEAP CORRUPTION DETECTED: after Normal block (#137) at 0x02198F90."</p>
  
  <p>Why would I be getting this error?</p>
  
  <p>Andrew L</p>