title: How to free up memory after converting a managed string to UTF-8 encoded unmanaged char*?
body: <p>I'm not familiar with C++/CLI so not sure how to free up the memory when using the code below (got the solution <a href="https://stackoverflow.com/questions/1271022/convert-from-c-cli-pointer-to-native-c-pointer">here</a> and modified a little):</p>
  
  <pre><code>char* ManagedStringToUnmanagedUTF8Char( String^ s )
  {
      array&lt;unsigned char&gt; ^bytes = Encoding::UTF8-&gt;GetBytes( s );
      pin_ptr&lt;unsigned char&gt; pinnedPtr = &amp;bytes[0];
      return (char*)pinnedPtr;
  }
  </code></pre>
  
  <p>The above code is working when I tested it by writing the char in a text file. Please let me know if I'm missing something (need to clean up pinnedPtr?).</p>
  
  <p>Now when I use it:</p>
  
  <pre><code>char* foobar = ManagedStringToUnmanagedUTF8Char("testing");
  
  //do something with foobar
  
  //do I need to free up memory by deleting foobar here? 
  //I tried 'delete foobar' or free(foobar) but it crashes my program
  </code></pre>