title: String marshalling and memory management
body: <p>Suppose I have the following C++ function that is exported in a library:</p>
  <pre><code>void foo(const wchar_t* text);
  </code></pre>
  <p>and C# code that uses that function:</p>
  <pre><code>[DllImport(&quot;bla.dll&quot;)]
  static extern void foo([MarshalAs(UnmanagedType.LPWStr)] string text);
  
  void Bar()
  {
      string s = &quot;hello&quot;;
      foo(s);
  }
  </code></pre>
  <ol>
  <li>Does the .net marshaller copy the string when I call <code>foo</code> or does the pointer point to the buffer of <code>s</code>?</li>
  <li>If the value is copied, when is it cleaned up?</li>
  <li>Do I have to clean it up myself (presumably inside <code>foo</code>)?</li>
  <li>If so, how do I clean up the memory?</li>
  </ol>
  <p>What's the behavior if <code>text</code> is marshalled as:</p>
  <ol>
  <li><code>UnmanagedType.LPWStr</code></li>
  <li><code>UnmanagedType.BStr</code></li>
  </ol>