title: How to properly marshal strings from Unity to C/C++?
body: <p>The following code snippet is from Unities Bonjour client example, which demonstrates how to interact with native code from C#. It's a simple C function that returns a string back to C# (Unity):</p>
  
  <pre><code>char* MakeStringCopy (const char* string)
  {
      if (string == NULL)
          return NULL;
  
      char* res = (char*)malloc(strlen(string) + 1);
      strcpy(res, string);
      return res;
  }
  
  const char* _GetLookupStatus ()
  {
      // By default mono string marshaler creates .Net string for returned UTF-8 C string 
      // and calls free for returned value, thus returned strings should be allocated on heap
      return MakeStringCopy([[delegateObject getStatus] UTF8String]);
  }
  </code></pre>
  
  <p>The C# declaration of the function looks like:</p>
  
  <pre><code>[DllImport ("__Internal")]
  private static extern string _GetLookupStatus ();
  </code></pre>
  
  <p>There are a few things that puzzle me here: </p>
  
  <ol>
  <li>Is this the right way to return a string from iOS native code to C#?</li>
  <li>How does the returned string ever get freed? </li>
  <li>Is there a better way to do it?</li>
  </ol>
  
  <p>Any insights in this matter are appreciated.
  Thank you.</p>