GCHandle.Alloc "Selects the normal descriptor for the specified object," which "creates a managed object descriptor ... that prevents the collection of the managed object."
, , System.Runtime.InteropServices.Marshal, , , , . , this, , ( , P/Invoke), . , :
APP_PARAM param = new APP_PARAM();
string[] text = new string[] { "A", "B", "C" };
param.numData = text.Length;
IntPtr arr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)) * text.Length);
try
{
param.text = arr;
IntPtr[] unmanagedText = new IntPtr[text.Length];
try
{
for (int i = 0; i < text.Length; i++)
unmanagedText[i] = Marshal.StringToHGlobalAnsi(text[i]);
Marshal.Copy(unmanagedText, 0, arr, unmanagedText.Length);
StartApp(param);
}
finally
{
foreach (IntPtr str in unmanagedText)
Marshal.FreeHGlobal(str);
}
}
finally
{
Marshal.FreeHGlobal(arr);
}
int / try/finally, , FreeHGlobal.