Before closing, your application needs to close any open wave file and release the DirectSoundNotify and DirectSound objects. Note that because you queried for the IDirectSoundNotify interface from a DirectSoundBuffer object, you must release the DirectSoundNotify object before releasing DirectSound. Releasing DirectSound automatically releases any existing buffers.
The following function does all the necessary cleanup:
void DSExit(void)
{
WaveCloseReadFile(&hmmio, &pwfx);
if (lpdsNotify)
lpdsNotify->Release();
if (lpds)
lpds->Release();
}