Passing array of strings from c# to c++ - memory management <p>In my code I have c DLL that accepts array of strings: void Helper::ProcessEvent(PEVENT_RECORD pEvent,wchar_t** OutPutFormattedData)</p> <p>I'm invoking it with this code:</p> <pre><code>[DllImport("Helper.dll", EntryPoint = "ProcessEvent")] internal static extern uint ProcessEvent( [In, Out] ref EVENT_RECORD pEvent, [In, Out] [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr)] ref string[] pResult); </code></pre> <p>In c++ code here is the main code I'm using to fill the array:</p> <pre><code>for(int i=0;i&lt;list.Size;i++) { EventWrapperClass *EWC = new EventWrapperClass(); EWC = list.Events[i]; OutPutFormattedData[i] = new wchar_t [wcslen(HelperFormatMessage(L"%s: %s\n",EWC-&gt;GetProperyName(),EWC-&gt;GetProperyValue()))+1]; wcscpy(OutPutFormattedData[i] ,HelperFormatMessage(L"%s: %s\n",EWC-&gt;GetProperyName(),EWC-&gt;GetProperyValue())); </code></pre> <p>}</p> <p>And the invoke code:</p> <pre><code>string[] strArr= new string[1]; NativeHelper.ProcessEvent(ref eventRecord, ref strArr); </code></pre> <p>I have two questions:</p> <ol> <li><p>Why when I check the value of the passed array in c# after calling this function I see it's empty (data is exist in c++ code, I debugged it) ?</p></li> <li><p>If I'm allocating memory in c++ dll, where I need to free it? in c++ or c#?</p></li> </ol> <p>Many thanks!</p> <p>Edit:</p> <p>signture of c++:</p> <pre><code>static __declspec(dllexport) void ProcessEvent(PEVENT_RECORD pEvent, wchar_t** OutPutFormattedData); </code></pre>