Author: silverblade Date: Sun Jul 6 10:20:40 2008 New Revision: 34333
URL: http://svn.reactos.org/svn/reactos?rev=34333&view=rev Log: Added cleanup code to sndblst.dll (for DriverProc/DRV_FREE), and memory allocation/free wrappers in MME-Buddy's utility module, to aid in checking for memory leaks. As a result, found and fixed a memory leak in RemoveSoundDevice.
Modified: branches/silverblade-audio/dll/win32/sndblst/sndblst.c branches/silverblade-audio/include/reactos/libs/sound/mmebuddy.h branches/silverblade-audio/lib/drivers/sound/mmebuddy/devices.c branches/silverblade-audio/lib/drivers/sound/mmebuddy/nt4.c branches/silverblade-audio/lib/drivers/sound/mmebuddy/utility.c
Modified: branches/silverblade-audio/dll/win32/sndblst/sndblst.c URL: http://svn.reactos.org/svn/reactos/branches/silverblade-audio/dll/win32/sndb... ============================================================================== --- branches/silverblade-audio/dll/win32/sndblst/sndblst.c [iso-8859-1] (original) +++ branches/silverblade-audio/dll/win32/sndblst/sndblst.c [iso-8859-1] Sun Jul 6 10:20:40 2008 @@ -43,13 +43,18 @@ SOUND_DEBUG(L"DRV_LOAD");
EnumerateNt4ServiceSoundDevices(L"sndblst", - WAVE_OUT_DEVICE_TYPE, + 0, FoundDevice);
return 1L;
case DRV_FREE : SOUND_DEBUG(L"DRV_FREE"); + + RemoveAllSoundDevices(); + + SOUND_DEBUG_HEX(GetMemoryAllocations()); + return 1L;
default : @@ -71,5 +76,7 @@
SOUND_DEBUG_HEX(wodMessage(0, WODM_GETNUMDEVS, 0, 0, 0));
+ DriverProc(0, 0, DRV_FREE, 0, 0); + return 0; }
Modified: branches/silverblade-audio/include/reactos/libs/sound/mmebuddy.h URL: http://svn.reactos.org/svn/reactos/branches/silverblade-audio/include/reacto... ============================================================================== --- branches/silverblade-audio/include/reactos/libs/sound/mmebuddy.h [iso-8859-1] (original) +++ branches/silverblade-audio/include/reactos/libs/sound/mmebuddy.h [iso-8859-1] Sun Jul 6 10:20:40 2008 @@ -48,11 +48,13 @@ Some memory allocation helper macros */
+/* #define AllocateMemory(size) \ HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size)
#define FreeMemory(ptr) \ HeapFree(GetProcessHeap(), 0, ptr) +*/
#define AllocateMemoryFor(thing) \ (thing*) AllocateMemory(sizeof(thing)) @@ -248,22 +250,21 @@ IN PSOUND_DEVICE SoundDevice, OUT LPWSTR* DevicePath);
-VOID -RemoveAllSoundDevices(); - -BOOLEAN -RemoveSoundDevices( - IN UCHAR DeviceType); - BOOLEAN AddSoundDevice( IN UCHAR DeviceType, IN PWSTR DevicePath);
-BOOLEAN +MMRESULT RemoveSoundDevice( - IN UCHAR DeviceType, - IN ULONG Index); + IN PSOUND_DEVICE SoundDevice); + +MMRESULT +RemoveSoundDevices( + IN UCHAR DeviceType); + +VOID +RemoveAllSoundDevices();
MMRESULT GetSoundDeviceType( @@ -364,6 +365,17 @@ utility.c */
+PVOID +AllocateMemory( + IN DWORD Size); + +VOID +FreeMemory( + IN PVOID Pointer); + +DWORD +GetMemoryAllocations(); + ULONG GetDigitCount( IN ULONG Number);
Modified: branches/silverblade-audio/lib/drivers/sound/mmebuddy/devices.c URL: http://svn.reactos.org/svn/reactos/branches/silverblade-audio/lib/drivers/so... ============================================================================== --- branches/silverblade-audio/lib/drivers/sound/mmebuddy/devices.c [iso-8859-1] (original) +++ branches/silverblade-audio/lib/drivers/sound/mmebuddy/devices.c [iso-8859-1] Sun Jul 6 10:20:40 2008 @@ -16,7 +16,9 @@
/* - TODO: Free up devicepath on exit + TODO: + The removal of devices from the list needs to be separated from + the destruction of the device structure. */
#include <windows.h> @@ -139,110 +141,96 @@ }
-BOOLEAN +MMRESULT RemoveSoundDevice( - IN UCHAR DeviceType, - IN ULONG Index) -{ - ULONG Counter = 0; + IN PSOUND_DEVICE SoundDevice) +{ ULONG TypeIndex; PSOUND_DEVICE CurrentDevice = NULL; PSOUND_DEVICE PreviousDevice = NULL;
- DPRINT("Removing a sound device from list %d\n", DeviceType); - - if ( ! VALID_SOUND_DEVICE_TYPE(DeviceType) ) - { - return FALSE; - } - - TypeIndex = DeviceType - MIN_SOUND_DEVICE_TYPE; - - CurrentDevice = SoundDeviceLists[TypeIndex]; - PreviousDevice = NULL; - - while ( CurrentDevice ) - { - if ( Counter == Index ) + /*DPRINT("Removing a sound device from list %d\n", DeviceType);*/ + + if ( ! SoundDevice ) + return MMSYSERR_INVALPARAM; + + TypeIndex = SoundDevice->DeviceType - MIN_SOUND_DEVICE_TYPE; + + /* Clean up any instances */ + if ( SoundDevice->FirstInstance != NULL ) + { + DestroyAllInstancesOfSoundDevice(SoundDevice); + } + + /* Close handle (if open) */ + if ( SoundDevice->Handle != INVALID_HANDLE_VALUE ) + { + CloseHandle(SoundDevice->Handle); + SoundDevice->Handle = INVALID_HANDLE_VALUE; + } + + if ( SoundDeviceLists[TypeIndex] == SoundDevice ) + { + SoundDeviceLists[TypeIndex] = SoundDevice->Next; + } + else + { + /* Remove from list */ + CurrentDevice = SoundDeviceLists[TypeIndex]; + PreviousDevice = NULL; + + while ( CurrentDevice ) { - /* Clean up any instances */ - if ( CurrentDevice->FirstInstance != NULL ) + if ( CurrentDevice == SoundDevice ) { - DestroyAllInstancesOfSoundDevice(CurrentDevice); + SOUND_ASSERT(PreviousDevice != NULL); + PreviousDevice->Next = CurrentDevice->Next; + + break; }
- /* Close handle (if open) */ - if ( CurrentDevice->Handle != INVALID_HANDLE_VALUE ) - { - CloseHandle(CurrentDevice->Handle); - CurrentDevice->Handle = INVALID_HANDLE_VALUE; - } - - if ( ! PreviousDevice ) - { - /* Head of list */ - SoundDeviceLists[TypeIndex] = CurrentDevice->Next; - } - else - { - /* Not the head of list */ - PreviousDevice->Next = CurrentDevice->Next; - } - - /* Free the memory associated with the device info */ - FreeMemory(CurrentDevice->DevicePath); - FreeMemory(CurrentDevice); - /*HeapFree(GetProcessHeap(), 0, CurrentDevice);*/ - CurrentDevice = NULL; - - DPRINT("Removal succeeded\n"); - - return TRUE; + PreviousDevice = CurrentDevice; + CurrentDevice = CurrentDevice->Next; } - - PreviousDevice = CurrentDevice; - ++ Counter; - } - - DPRINT("Not found\n"); - /* Not found */ - return FALSE; -} - - -BOOLEAN + } + + /* Free the memory associated with the device info */ + FreeMemory(SoundDevice->DevicePath); + FreeMemory(SoundDevice); + + return MMSYSERR_NOERROR;; +} + + +MMRESULT RemoveSoundDevices( IN UCHAR DeviceType) { PSOUND_DEVICE CurrentDevice; - PSOUND_DEVICE NextDevice;
DPRINT("Emptying device list for device type %d\n", DeviceType);
if ( ! VALID_SOUND_DEVICE_TYPE(DeviceType) ) { DPRINT("Invalid device type - %d\n", DeviceType); - return FALSE; - } - - /* Clean out the device list */ - CurrentDevice = SoundDeviceLists[DeviceType - MIN_SOUND_DEVICE_TYPE]; - - while ( CurrentDevice ) - { - /* Save the next device pointer so we can reference it later */ - NextDevice = CurrentDevice->Next; - - FreeMemory(CurrentDevice); - /*HeapFree(GetProcessHeap(), 0, CurrentDevice);*/ - CurrentDevice = NextDevice; + return MMSYSERR_INVALPARAM; + } + + /* + Clean out the device list. This works by repeatedly removing the + first entry. + */ + while ( (CurrentDevice = + SoundDeviceLists[DeviceType - MIN_SOUND_DEVICE_TYPE]) ) + { + RemoveSoundDevice(CurrentDevice); }
/* Reset the list content and item count */ SoundDeviceLists[DeviceType - MIN_SOUND_DEVICE_TYPE] = NULL; SoundDeviceTotals[DeviceType - MIN_SOUND_DEVICE_TYPE] = 0;
- return TRUE; + return MMSYSERR_NOERROR; }
@@ -253,7 +241,7 @@
DPRINT("Emptying all device lists\n");
- for ( i = 0; i < SOUND_DEVICE_TYPES; ++ i ) + for ( i = MIN_SOUND_DEVICE_TYPE; i <= MAX_SOUND_DEVICE_TYPE; ++ i ) { RemoveSoundDevices(i); }
Modified: branches/silverblade-audio/lib/drivers/sound/mmebuddy/nt4.c URL: http://svn.reactos.org/svn/reactos/branches/silverblade-audio/lib/drivers/so... ============================================================================== --- branches/silverblade-audio/lib/drivers/sound/mmebuddy/nt4.c [iso-8859-1] (original) +++ branches/silverblade-audio/lib/drivers/sound/mmebuddy/nt4.c [iso-8859-1] Sun Jul 6 10:20:40 2008 @@ -167,7 +167,9 @@ if ( ! ServiceName ) return MMSYSERR_INVALPARAM;
- if ( ! VALID_SOUND_DEVICE_TYPE(DeviceType) ) + /* Device type zero means "all" */ + if ( ( ! VALID_SOUND_DEVICE_TYPE(DeviceType) ) && + ( DeviceType != 0 ) ) return MMSYSERR_INVALPARAM;
while ( OpenSoundDeviceRegKey(ServiceName, KeyIndex, &Key) == MMSYSERR_NOERROR )
Modified: branches/silverblade-audio/lib/drivers/sound/mmebuddy/utility.c URL: http://svn.reactos.org/svn/reactos/branches/silverblade-audio/lib/drivers/so... ============================================================================== --- branches/silverblade-audio/lib/drivers/sound/mmebuddy/utility.c [iso-8859-1] (original) +++ branches/silverblade-audio/lib/drivers/sound/mmebuddy/utility.c [iso-8859-1] Sun Jul 6 10:20:40 2008 @@ -14,6 +14,56 @@
#include <windows.h> #include <mmsystem.h> + +#include <mmebuddy.h> + +/* + Memory +*/ + +static HANDLE ProcessHeapHandle = INVALID_HANDLE_VALUE; +static DWORD CurrentAllocations = 0; + +PVOID +AllocateMemory( + IN DWORD Size) +{ + PVOID Pointer = NULL; + + if ( ProcessHeapHandle == INVALID_HANDLE_VALUE ) + ProcessHeapHandle = GetProcessHeap(); + + Pointer = HeapAlloc(ProcessHeapHandle, HEAP_ZERO_MEMORY, Size); + + if ( ! Pointer ) + return NULL; + + ++ CurrentAllocations; + + return Pointer; +} + +VOID +FreeMemory( + IN PVOID Pointer) +{ + SOUND_ASSERT(ProcessHeapHandle != INVALID_HANDLE_VALUE); + + HeapFree(ProcessHeapHandle, 0, Pointer); + + -- CurrentAllocations; +} + +DWORD +GetMemoryAllocations() +{ + return CurrentAllocations; +} + + +/* + Other +*/
ULONG GetDigitCount(