I use NAudio in my application, it almost allways runs good, but sometimes it crashes.I read the code of NAudio, and i found maybe the crash is caused by the memory leak in the following code.
```
public static IList<MixerControl> GetMixerControls(IntPtr mixerHandle, MixerLine mixerLine, MixerFlags mixerHandleType)
{
List<MixerControl> controls = new List<MixerControl>();
if (mixerLine.ControlsCount > 0)
{
int mixerControlSize = Marshal.SizeOf(typeof(MixerInterop.MIXERCONTROL));
MixerInterop.MIXERLINECONTROLS mlc = default(MixerInterop.MIXERLINECONTROLS);
IntPtr pmc = Marshal.AllocHGlobal(mixerControlSize * mixerLine.ControlsCount);
mlc.cbStruct = Marshal.SizeOf(mlc);
mlc.dwLineID = mixerLine.LineId;
mlc.cControls = mixerLine.ControlsCount;
mlc.pamxctrl = pmc;
mlc.cbmxctrl = Marshal.SizeOf(typeof(MixerInterop.MIXERCONTROL));
MmResult err = MixerInterop.mixerGetLineControls(mixerHandle, ref mlc, mixerHandleType);
if (err != MmResult.NoError)
{
Marshal.FreeHGlobal(pmc);
throw new MmException(err, "mixerGetLineControls");
}
for (int i = 0; i < mlc.cControls; i++)
{
long address = pmc.ToInt64() + (long)(mixerControlSize * i);
MixerInterop.MIXERCONTROL mc = (MixerInterop.MIXERCONTROL)Marshal.PtrToStructure((IntPtr)address, typeof(MixerInterop.MIXERCONTROL));
MixerControl mixerControl = MixerControl.GetMixerControl(mixerHandle, mixerLine.LineId, mc.dwControlID, mixerLine.Channels, mixerHandleType);
controls.Add(mixerControl);
}
}
return controls;
}
```
In the code above, it uses Marshal.AllocHGlobal to allocate memory for pmc, but when the function normally returns, the Marshal.FreeHGlobal is not called.
I think we can improve the code as below:
```
public static IList<MixerControl> GetMixerControls(IntPtr mixerHandle, MixerLine mixerLine, MixerFlags mixerHandleType)
{
List<MixerControl> controls = new List<MixerControl>();
if (mixerLine.ControlsCount > 0)
{
int mixerControlSize = Marshal.SizeOf(typeof(MixerInterop.MIXERCONTROL));
MixerInterop.MIXERLINECONTROLS mlc = default(MixerInterop.MIXERLINECONTROLS);
IntPtr pmc = Marshal.AllocHGlobal(mixerControlSize * mixerLine.ControlsCount);
mlc.cbStruct = Marshal.SizeOf(mlc);
mlc.dwLineID = mixerLine.LineId;
mlc.cControls = mixerLine.ControlsCount;
mlc.pamxctrl = pmc;
mlc.cbmxctrl = Marshal.SizeOf(typeof(MixerInterop.MIXERCONTROL));
MmResult err = MixerInterop.mixerGetLineControls(mixerHandle, ref mlc, mixerHandleType);
if (err != MmResult.NoError)
{
Marshal.FreeHGlobal(pmc);
throw new MmException(err, "mixerGetLineControls");
}
for (int i = 0; i < mlc.cControls; i++)
{
long address = pmc.ToInt64() + (long)(mixerControlSize * i);
MixerInterop.MIXERCONTROL mc = (MixerInterop.MIXERCONTROL)Marshal.PtrToStructure((IntPtr)address, typeof(MixerInterop.MIXERCONTROL));
MixerControl mixerControl = MixerControl.GetMixerControl(mixerHandle, mixerLine.LineId, mc.dwControlID, mixerLine.Channels, mixerHandleType);
controls.Add(mixerControl);
}
Marshal.FreeHGlobal(pmc);
}
return controls;
}
```
And I hope it will help the ones who encounter the same problem with me.
Comments: thanks for reporting, have fixed with a finally statement. Let me know if this works for you
```
public static IList<MixerControl> GetMixerControls(IntPtr mixerHandle, MixerLine mixerLine, MixerFlags mixerHandleType)
{
List<MixerControl> controls = new List<MixerControl>();
if (mixerLine.ControlsCount > 0)
{
int mixerControlSize = Marshal.SizeOf(typeof(MixerInterop.MIXERCONTROL));
MixerInterop.MIXERLINECONTROLS mlc = default(MixerInterop.MIXERLINECONTROLS);
IntPtr pmc = Marshal.AllocHGlobal(mixerControlSize * mixerLine.ControlsCount);
mlc.cbStruct = Marshal.SizeOf(mlc);
mlc.dwLineID = mixerLine.LineId;
mlc.cControls = mixerLine.ControlsCount;
mlc.pamxctrl = pmc;
mlc.cbmxctrl = Marshal.SizeOf(typeof(MixerInterop.MIXERCONTROL));
MmResult err = MixerInterop.mixerGetLineControls(mixerHandle, ref mlc, mixerHandleType);
if (err != MmResult.NoError)
{
Marshal.FreeHGlobal(pmc);
throw new MmException(err, "mixerGetLineControls");
}
for (int i = 0; i < mlc.cControls; i++)
{
long address = pmc.ToInt64() + (long)(mixerControlSize * i);
MixerInterop.MIXERCONTROL mc = (MixerInterop.MIXERCONTROL)Marshal.PtrToStructure((IntPtr)address, typeof(MixerInterop.MIXERCONTROL));
MixerControl mixerControl = MixerControl.GetMixerControl(mixerHandle, mixerLine.LineId, mc.dwControlID, mixerLine.Channels, mixerHandleType);
controls.Add(mixerControl);
}
}
return controls;
}
```
In the code above, it uses Marshal.AllocHGlobal to allocate memory for pmc, but when the function normally returns, the Marshal.FreeHGlobal is not called.
I think we can improve the code as below:
```
public static IList<MixerControl> GetMixerControls(IntPtr mixerHandle, MixerLine mixerLine, MixerFlags mixerHandleType)
{
List<MixerControl> controls = new List<MixerControl>();
if (mixerLine.ControlsCount > 0)
{
int mixerControlSize = Marshal.SizeOf(typeof(MixerInterop.MIXERCONTROL));
MixerInterop.MIXERLINECONTROLS mlc = default(MixerInterop.MIXERLINECONTROLS);
IntPtr pmc = Marshal.AllocHGlobal(mixerControlSize * mixerLine.ControlsCount);
mlc.cbStruct = Marshal.SizeOf(mlc);
mlc.dwLineID = mixerLine.LineId;
mlc.cControls = mixerLine.ControlsCount;
mlc.pamxctrl = pmc;
mlc.cbmxctrl = Marshal.SizeOf(typeof(MixerInterop.MIXERCONTROL));
MmResult err = MixerInterop.mixerGetLineControls(mixerHandle, ref mlc, mixerHandleType);
if (err != MmResult.NoError)
{
Marshal.FreeHGlobal(pmc);
throw new MmException(err, "mixerGetLineControls");
}
for (int i = 0; i < mlc.cControls; i++)
{
long address = pmc.ToInt64() + (long)(mixerControlSize * i);
MixerInterop.MIXERCONTROL mc = (MixerInterop.MIXERCONTROL)Marshal.PtrToStructure((IntPtr)address, typeof(MixerInterop.MIXERCONTROL));
MixerControl mixerControl = MixerControl.GetMixerControl(mixerHandle, mixerLine.LineId, mc.dwControlID, mixerLine.Channels, mixerHandleType);
controls.Add(mixerControl);
}
Marshal.FreeHGlobal(pmc);
}
return controls;
}
```
And I hope it will help the ones who encounter the same problem with me.
Comments: thanks for reporting, have fixed with a finally statement. Let me know if this works for you