AviWriter.cs : don't mix unmanaged allocation types

This commit is contained in:
goyuken 2012-08-03 22:02:04 +00:00
parent 49daf880fb
commit b14154b30c
1 changed files with 41 additions and 14 deletions

View File

@ -328,27 +328,49 @@ namespace BizHawk.MultiClient
private CodecToken() { } private CodecToken() { }
public Win32.AVICOMPRESSOPTIONS comprOptions; public Win32.AVICOMPRESSOPTIONS comprOptions;
public string codec; public string codec;
/// <summary>
/// true if data was allocated by AviSaveOptions and should be freed by AVISaveOptionsFree
/// </summary>
bool allocated = false; bool allocated = false;
/// <summary>
/// true if data was allocated by AllocHGlobal and should be freed by FreeHGlobal
/// </summary>
bool marshaled = false;
public void Dispose() public void Dispose()
{ {
if (!allocated) return; if (allocated)
{
IntPtr[] infPtrs = new IntPtr[1];
IntPtr mem;
IntPtr[] infPtrs = new IntPtr[1]; // alloc unmanaged memory
IntPtr mem; mem = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Win32.AVICOMPRESSOPTIONS)));
infPtrs[0] = mem;
// alloc unmanaged memory // copy from managed structure to unmanaged memory
mem = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Win32.AVICOMPRESSOPTIONS))); Marshal.StructureToPtr(comprOptions, mem, false);
infPtrs[0] = mem;
// copy from managed structure to unmanaged memory Win32.AVISaveOptionsFree(1, infPtrs);
Marshal.StructureToPtr(comprOptions, mem, false); Marshal.FreeHGlobal(mem);
Win32.AVISaveOptionsFree(1, infPtrs); codec = null;
Marshal.FreeHGlobal(mem); 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; codec = null;
comprOptions = new Win32.AVICOMPRESSOPTIONS(); comprOptions = new Win32.AVICOMPRESSOPTIONS();
allocated = false; marshaled = false;
}
} }
byte[] SerializeToByteArray() byte[] SerializeToByteArray()
@ -437,7 +459,12 @@ namespace BizHawk.MultiClient
} }
else else
comprOptions.lpParms = (int)IntPtr.Zero; 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() public string Serialize()