From b14154b30ca3945d96803712fa5942962007af79 Mon Sep 17 00:00:00 2001 From: goyuken Date: Fri, 3 Aug 2012 22:02:04 +0000 Subject: [PATCH] AviWriter.cs : don't mix unmanaged allocation types --- BizHawk.MultiClient/AVOut/AviWriter.cs | 55 +++++++++++++++++++------- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/BizHawk.MultiClient/AVOut/AviWriter.cs b/BizHawk.MultiClient/AVOut/AviWriter.cs index 0a8bc57ccc..ca6389edb9 100644 --- a/BizHawk.MultiClient/AVOut/AviWriter.cs +++ b/BizHawk.MultiClient/AVOut/AviWriter.cs @@ -328,27 +328,49 @@ namespace BizHawk.MultiClient private CodecToken() { } public Win32.AVICOMPRESSOPTIONS comprOptions; public string codec; + /// + /// true if data was allocated by AviSaveOptions and should be freed by AVISaveOptionsFree + /// bool allocated = false; + /// + /// true if data was allocated by AllocHGlobal and should be freed by FreeHGlobal + /// + bool marshaled = false; public void Dispose() { - if (!allocated) return; + if (allocated) + { + IntPtr[] infPtrs = new IntPtr[1]; + IntPtr mem; - IntPtr[] infPtrs = new IntPtr[1]; - IntPtr mem; + // alloc unmanaged memory + mem = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Win32.AVICOMPRESSOPTIONS))); + infPtrs[0] = mem; - // alloc unmanaged memory - mem = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Win32.AVICOMPRESSOPTIONS))); - infPtrs[0] = mem; + // copy from managed structure to unmanaged memory + Marshal.StructureToPtr(comprOptions, mem, false); - // copy from managed structure to unmanaged memory - Marshal.StructureToPtr(comprOptions, mem, false); + Win32.AVISaveOptionsFree(1, infPtrs); + Marshal.FreeHGlobal(mem); - Win32.AVISaveOptionsFree(1, infPtrs); - Marshal.FreeHGlobal(mem); + codec = null; + comprOptions = new Win32.AVICOMPRESSOPTIONS(); + allocated = false; + } + if (marshaled) + { + IntPtr p; + p = (IntPtr)comprOptions.lpFormat; + if (p != IntPtr.Zero) + Marshal.FreeHGlobal(p); + p = (IntPtr)comprOptions.lpParms; + if (p != IntPtr.Zero) + Marshal.FreeHGlobal(p); - codec = null; - comprOptions = new Win32.AVICOMPRESSOPTIONS(); - allocated = false; + codec = null; + comprOptions = new Win32.AVICOMPRESSOPTIONS(); + marshaled = false; + } } byte[] SerializeToByteArray() @@ -437,7 +459,12 @@ namespace BizHawk.MultiClient } else comprOptions.lpParms = (int)IntPtr.Zero; - return CodecToken.TakePossession(comprOptions); + + CodecToken ret = new CodecToken(); + ret.marshaled = true; + ret.comprOptions = comprOptions; + ret.codec = Win32.decode_mmioFOURCC(comprOptions.fccHandler); + return ret; } public string Serialize()