From 2e9752838d11fbb3b9ed374659ce34a77b13ce66 Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Mon, 20 Jul 2015 10:03:42 -0400 Subject: [PATCH] Fix Win32 UTF8 marshaling --- src/LPUtf8StrMarshaler.cs | 4 ++-- src/SDL2.cs | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/LPUtf8StrMarshaler.cs b/src/LPUtf8StrMarshaler.cs index bc28039..ca91f74 100644 --- a/src/LPUtf8StrMarshaler.cs +++ b/src/LPUtf8StrMarshaler.cs @@ -80,7 +80,7 @@ namespace SDL2 throw new ArgumentException("ManagedObj must be a string.", "ManagedObj"); } var bytes = Encoding.UTF8.GetBytes(str); - var mem = Marshal.AllocHGlobal(bytes.Length + 1); + var mem = SDL.SDL_malloc((IntPtr) bytes.Length + 1); Marshal.Copy(bytes, 0, mem, bytes.Length); ((byte*)mem)[bytes.Length] = 0; return mem; @@ -94,7 +94,7 @@ namespace SDL2 { if (!_leaveAllocated) { - Marshal.FreeHGlobal(pNativeData); + SDL.SDL_free(pNativeData); } } diff --git a/src/SDL2.cs b/src/SDL2.cs index 74e3564..99323f1 100644 --- a/src/SDL2.cs +++ b/src/SDL2.cs @@ -60,6 +60,14 @@ namespace SDL2 SDL_TRUE = 1 } + /* malloc/free are used by the marshaler! -flibit */ + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + internal static extern IntPtr SDL_malloc(IntPtr size); + + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + internal static extern void SDL_free(IntPtr memblock); + #endregion #region SDL_rwops.h