Fixes bugs in String marshalling.

Fixes two issues:
1. FreeStringArrayPtr used the wrong variable in the offset to
ReadIntPtr causing an access violation.
2. Better cleanup of memory in MarshalStringArrayToPtr when any alloc
fails.
This commit is contained in:
Fraser Waters 2014-06-19 22:51:47 +01:00
parent b9e948580a
commit 22760a4032

View file

@ -202,12 +202,27 @@ namespace OpenTK
throw new OutOfMemoryException(); throw new OutOfMemoryException();
} }
for (int i = 0; i < str_array.Length; i++) int i = 0;
try
{
for (i = 0; i < str_array.Length; i++)
{ {
IntPtr str = MarshalStringToPtr(str_array[i]); IntPtr str = MarshalStringToPtr(str_array[i]);
Marshal.WriteIntPtr(ptr, i * IntPtr.Size, str); Marshal.WriteIntPtr(ptr, i * IntPtr.Size, str);
} }
} }
catch (OutOfMemoryException oom)
{
for (i = i - 1; i >= 0; --i)
{
Marshal.FreeHGlobal(Marshal.ReadIntPtr(ptr, i * IntPtr.Size));
}
Marshal.FreeHGlobal(ptr);
throw oom;
}
}
return ptr; return ptr;
} }
@ -220,7 +235,7 @@ namespace OpenTK
{ {
for (int i = 0; i < length; i++) for (int i = 0; i < length; i++)
{ {
Marshal.FreeHGlobal(Marshal.ReadIntPtr(ptr, length * IntPtr.Size)); Marshal.FreeHGlobal(Marshal.ReadIntPtr(ptr, i * IntPtr.Size));
} }
Marshal.FreeHGlobal(ptr); Marshal.FreeHGlobal(ptr);
} }