GB: cleanup and fix memory leaks for a few odd cases

This commit is contained in:
goyuken 2013-12-09 20:50:21 +00:00
parent 2a0217e06c
commit 81996af187
1 changed files with 44 additions and 39 deletions

View File

@ -62,43 +62,51 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy
if (GambatteState == IntPtr.Zero) if (GambatteState == IntPtr.Zero)
throw new Exception("gambatte_create() returned null???"); throw new Exception("gambatte_create() returned null???");
LibGambatte.LoadFlags flags = 0; try
{
LibGambatte.LoadFlags flags = 0;
if (game["ForceDMG"]) if (game["ForceDMG"])
flags |= LibGambatte.LoadFlags.FORCE_DMG; flags |= LibGambatte.LoadFlags.FORCE_DMG;
if (game["GBACGB"]) if (game["GBACGB"])
flags |= LibGambatte.LoadFlags.GBA_CGB; flags |= LibGambatte.LoadFlags.GBA_CGB;
if (game["MulitcartCompat"]) if (game["MulitcartCompat"])
flags |= LibGambatte.LoadFlags.MULTICART_COMPAT; flags |= LibGambatte.LoadFlags.MULTICART_COMPAT;
if (LibGambatte.gambatte_load(GambatteState, romdata, (uint)romdata.Length, GetCurrentTime(), flags) != 0) if (LibGambatte.gambatte_load(GambatteState, romdata, (uint)romdata.Length, GetCurrentTime(), flags) != 0)
throw new Exception("gambatte_load() returned non-zero (is this not a gb or gbc rom?)"); throw new Exception("gambatte_load() returned non-zero (is this not a gb or gbc rom?)");
// set real default colors (before anyone mucks with them at all) // set real default colors (before anyone mucks with them at all)
ChangeDMGColors(new int[] { 10798341, 8956165, 1922333, 337157, 10798341, 8956165, 1922333, 337157, 10798341, 8956165, 1922333, 337157 }); ChangeDMGColors(new int[] { 10798341, 8956165, 1922333, 337157, 10798341, 8956165, 1922333, 337157, 10798341, 8956165, 1922333, 337157 });
SetCGBColors(GBColors.ColorType.gambatte); SetCGBColors(GBColors.ColorType.gambatte);
InitSound(); InitSound();
Frame = 0; Frame = 0;
LagCount = 0; LagCount = 0;
IsLagFrame = false; IsLagFrame = false;
InputCallback = new LibGambatte.InputGetter(ControllerCallback); InputCallback = new LibGambatte.InputGetter(ControllerCallback);
LibGambatte.gambatte_setinputgetter(GambatteState, InputCallback); LibGambatte.gambatte_setinputgetter(GambatteState, InputCallback);
InitMemoryDomains(); InitMemoryDomains();
CoreComm.RomStatusDetails = string.Format("{0}\r\nSHA1:{1}\r\nMD5:{2}\r\n", CoreComm.RomStatusDetails = string.Format("{0}\r\nSHA1:{1}\r\nMD5:{2}\r\n",
game.Name, game.Name,
Util.BytesToHexString(System.Security.Cryptography.SHA1.Create().ComputeHash(romdata)), Util.BytesToHexString(System.Security.Cryptography.SHA1.Create().ComputeHash(romdata)),
Util.BytesToHexString(System.Security.Cryptography.MD5.Create().ComputeHash(romdata)) Util.BytesToHexString(System.Security.Cryptography.MD5.Create().ComputeHash(romdata))
); );
TimeCallback = new LibGambatte.RTCCallback(GetCurrentTime); TimeCallback = new LibGambatte.RTCCallback(GetCurrentTime);
LibGambatte.gambatte_setrtccallback(GambatteState, TimeCallback); LibGambatte.gambatte_setrtccallback(GambatteState, TimeCallback);
}
catch
{
Dispose();
throw;
}
} }
public static readonly ControllerDefinition GbController = new ControllerDefinition public static readonly ControllerDefinition GbController = new ControllerDefinition
@ -740,8 +748,11 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy
public void Dispose() public void Dispose()
{ {
LibGambatte.gambatte_destroy(GambatteState); if (GambatteState != IntPtr.Zero)
GambatteState = IntPtr.Zero; {
LibGambatte.gambatte_destroy(GambatteState);
GambatteState = IntPtr.Zero;
}
DisposeSound(); DisposeSound();
} }
@ -826,9 +837,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy
int latchaudio = 0; int latchaudio = 0;
//SpeexResampler resampler;
//DCFilter dcfilter;
BlipBuffer blip; BlipBuffer blip;
void ProcessSound() void ProcessSound()
@ -858,20 +866,17 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy
void InitSound() void InitSound()
{ {
//resampler = new Sound.Utilities.SpeexResampler(2, 2097152, 44100, 2097152, 44100, null, this);
//dcfilter = Sound.Utilities.DCFilter.AsISyncSoundProvider(resampler, 65536);
// lowpass filtering on an actual GB was probably pretty aggressive?
//dcfilter = Sound.Utilities.DCFilter.AsISyncSoundProvider(resampler, 2048);
blip = new BlipBuffer(1024); blip = new BlipBuffer(1024);
blip.SetRates(2097152, 44100); blip.SetRates(2097152, 44100);
} }
void DisposeSound() void DisposeSound()
{ {
blip.Dispose(); if (blip != null)
blip = null; {
//resampler.Dispose(); blip.Dispose();
//resampler = null; blip = null;
}
} }
public void DiscardSamples() public void DiscardSamples()