title: Returning a std::string from a C++ DLL to a c# program -> Invalid Address specified to RtlFreeHeap
body: <p>In a function in my C++ DLL, I'm returning a std::string to my c# application. It pretty much looks like this:</p>
  
  <pre><code>std::string g_DllName = "MyDLL";
  
  extern "C" THUNDER_API const char* __stdcall GetDLLName()
  {
      return g_DllName.c_str();
  }
  </code></pre>
  
  <p>But when my C# code calls this function, I get this message in my output window:</p>
  
  <pre><code>Invalid Address specified to RtlFreeHeap( 00150000, 0012D8D8 )
  </code></pre>
  
  <p>The function declaration in c# looks like this:</p>
  
  <pre><code>[DllImport("MyDll", EntryPoint = "GetDLLName")]
      [return: MarshalAs(UnmanagedType.LPStr)]
      public static extern string GetDLLName();
  </code></pre>
  
  <p>From what I've been able to find online, sometimes this message appears when there's an inconsistency between which version of new(debug or release, etc) is being used with delete. But I'm not sure if that is what is going on in my case. So I'm not sure exactly what's causing it. Maybe the MashallAs might have something to do with it? </p>
  
  <p>Any ideas?</p>
  
  <p>Thanks!</p>