Skip to content

Commit

Permalink
Fixes bugs in String marshalling.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Frassle committed Jun 19, 2014
1 parent b9e9485 commit 22760a4
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions Source/OpenTK/BindingsBase.cs
Expand Up @@ -202,10 +202,25 @@ protected static IntPtr MarshalStringArrayToPtr(string[] str_array)
throw new OutOfMemoryException(); throw new OutOfMemoryException();
} }


for (int i = 0; i < str_array.Length; i++) int i = 0;
try
{ {
IntPtr str = MarshalStringToPtr(str_array[i]); for (i = 0; i < str_array.Length; i++)
Marshal.WriteIntPtr(ptr, i * IntPtr.Size, str); {
IntPtr str = MarshalStringToPtr(str_array[i]);
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;
Expand All @@ -220,7 +235,7 @@ protected static void FreeStringArrayPtr(IntPtr ptr, int length)
{ {
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);
} }
Expand Down

0 comments on commit 22760a4

Please sign in to comment.