title: Passing byte array between C++ and C# ByRef raises AccessViolationException
body: <p>I am trying to create a Win32 DLL exposes some functions which are called in C# as follows</p>
  
  <pre><code>__declspec(dllexport) int GetData(unsigned char* *data, int* size)
  {
      try
      {
          int tlen = 3;
          unsigned char* tchr = new unsigned char[5];
          tchr[0] = 'a';
          tchr[1] = 'b';
          tchr[2] = 'c';
  
          *size = tlen;
          *data = tchr;
  
          return 1;
      }
      catch (char *p)
      {
          return 0;
      }
  }
  </code></pre>
  
  <p>And on C# side</p>
  
  <pre><code>[DllImport("MyDll.dll")]
  static extern int GetData(ref byte[] data, ref int size);
  
  static void Main()
  {
      try
      {
          int hr = 0;
          byte[] gData = null;
          int gSize = 0;
          hr = GetData(ref gData, ref gSize);
          Console.WriteLine(gSize);
          for (int i = 0; i &lt; gSize; i++)
              Console.WriteLine((char)gData[i]);
      }
      catch (Exception p)
      {
          Console.WriteLine(p.ToString());
      }
  }
  </code></pre>
  
  <p>When I run C# code, <code>AccessViolationException</code> happens on <code>GetData</code> function which is a sign of exception in C++ code however, following C++ code snippet works fine without any error.</p>
  
  <pre><code>int _tmain(int argc, _TCHAR* argv[])
  {
      unsigned char* data = NULL;
      int size = NULL;
      GetData(&amp;data, &amp;size);
      printf("%d", size);
      for (int i = 0; i &lt; size; i++)
          printf("%c,", data[i]);
      return 0;
  }
  </code></pre>
  
  <p>If you compare C# <code>main</code> function and C++ <code>_tmain</code>, they are almost analoguous so where I may make a mistake?</p>