Permalink
Browse files

Merge pull request #141 from Frassle/marshal

Fixes bugs in String marshalling.
  • Loading branch information...
thefiddler committed Jun 20, 2014
2 parents f0fd13a + 22760a4 commit 4acbc2268dce7e984d64296225917271131c7e75
Showing with 19 additions and 4 deletions.
  1. +19 −4 Source/OpenTK/BindingsBase.cs
@@ -202,10 +202,25 @@ protected static IntPtr MarshalStringArrayToPtr(string[] str_array)
throw new OutOfMemoryException();
}
- for (int i = 0; i < str_array.Length; i++)
+ int i = 0;
+ try
{
- IntPtr str = MarshalStringToPtr(str_array[i]);
- Marshal.WriteIntPtr(ptr, i * IntPtr.Size, str);
+ for (i = 0; i < str_array.Length; i++)
+ {
+ 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;
@@ -220,7 +235,7 @@ protected static void FreeStringArrayPtr(IntPtr ptr, int length)
{
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);
}

0 comments on commit 4acbc22

Please sign in to comment.