title: Why would waveOutWrite() cause an exception in the debug heap?
body: <p>While researching this issue, I found multiple mentions of the following scenario online, invariably as unanswered questions on programming forums. I hope that posting this here will at least serve to document my findings.</p>
  
  <p>First, the symptom: While running pretty standard code that uses waveOutWrite() to output PCM audio, I sometimes get this when running under the debugger:</p>
  
  <pre><code> ntdll.dll!_DbgBreakPoint@0()   
   ntdll.dll!_RtlpBreakPointHeap@4()  + 0x28 bytes    
   ntdll.dll!_RtlpValidateHeapEntry@12()  + 0x113 bytes   
   ntdll.dll!_RtlDebugGetUserInfoHeap@20()  + 0x96 bytes  
   ntdll.dll!_RtlGetUserInfoHeap@20()  + 0x32743 bytes    
   kernel32.dll!_GlobalHandle@4()  + 0x3a bytes   
   wdmaud.drv!_waveCompleteHeader@4()  + 0x40 bytes   
   wdmaud.drv!_waveThread@4()  + 0x9c bytes   
   kernel32.dll!_BaseThreadStart@8()  + 0x37 bytes    
  </code></pre>
  
  <p>While the obvious suspect would be a heap corruption somewhere else in the code, I found out that that's not the case. Furthermore, I was able to reproduce this problem using the following code (this is part of a dialog based MFC application:)</p>
  
  <pre><code>void CwaveoutDlg::OnBnClickedButton1()
  {
      WAVEFORMATEX wfx;
      wfx.nSamplesPerSec = 44100; /* sample rate */
      wfx.wBitsPerSample = 16; /* sample size */
      wfx.nChannels = 2;
      wfx.cbSize = 0; /* size of _extra_ info */
      wfx.wFormatTag = WAVE_FORMAT_PCM;
      wfx.nBlockAlign = (wfx.wBitsPerSample &gt;&gt; 3) * wfx.nChannels;
      wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;
  
      waveOutOpen(&amp;hWaveOut, 
                  WAVE_MAPPER, 
                  &amp;wfx,  
                  (DWORD_PTR)m_hWnd, 
                  0,
                  CALLBACK_WINDOW );
  
      ZeroMemory(&amp;header, sizeof(header));
      header.dwBufferLength = 4608;
      header.lpData = (LPSTR)GlobalLock(GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE | GMEM_ZEROINIT, 4608));
  
      waveOutPrepareHeader(hWaveOut, &amp;header, sizeof(header));
      waveOutWrite(hWaveOut, &amp;header, sizeof(header));
  }
  
  afx_msg LRESULT CwaveoutDlg::OnWOMDone(WPARAM wParam, LPARAM lParam)
  {
      HWAVEOUT dev = (HWAVEOUT)wParam;
      WAVEHDR *hdr = (WAVEHDR*)lParam;
      waveOutUnprepareHeader(dev, hdr, sizeof(WAVEHDR));
      GlobalFree(GlobalHandle(hdr-&gt;lpData));
      ZeroMemory(hdr, sizeof(*hdr));
      hdr-&gt;dwBufferLength = 4608;
      hdr-&gt;lpData = (LPSTR)GlobalLock(GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE | GMEM_ZEROINIT, 4608));
      waveOutPrepareHeader(hWaveOut, &amp;header, sizeof(WAVEHDR));
      waveOutWrite(hWaveOut, hdr, sizeof(WAVEHDR));
      return 0;
   }
  </code></pre>
  
  <p>Before anyone comments on this, yes - the sample code plays back uninitialized memory. Don't try this with your speakers turned all the way up.</p>
  
  <p>Some debugging revealed the following information: waveOutPrepareHeader() populates header.reserved with a pointer to what appears to be a structure containing at least two pointers as its first two members. The first pointer is set to NULL. After calling waveOutWrite(), this pointer is set to a pointer allocated on the global heap. In pseudo code, that would look something like this:</p>
  
  <pre><code>struct Undocumented { void *p1, *p2; } /* This might have more members */
  
  MMRESULT waveOutPrepareHeader( handle, LPWAVEHDR hdr, ...) {
      hdr-&gt;reserved = (Undocumented*)calloc(sizeof(Undocumented));
      /* Do more stuff... */
  }
  
  MMRESULT waveOutWrite( handle, LPWAVEHDR hdr, ...) {
  
      /* The following assignment fails rarely, causing the problem: */
      hdr-&gt;reserved-&gt;p1 = malloc( /* chunk of private data */ );
      /* Probably more code to initiate playback */
  }
  </code></pre>
  
  <p>Normally, the header is returned to the application by waveCompleteHeader(), a function internal to wdmaud.dll. waveCompleteHeader() tries to deallocate the pointer allocated by waveOutWrite() by calling GlobalHandle()/GlobalUnlock() and friends. Sometimes, GlobalHandle() bombs, as shown above.</p>
  
  <p>Now, the reason that GlobalHandle() bombs is not due to a heap corruption, as I suspected at first - it's because waveOutWrite() returned without setting the first pointer in the internal structure to a valid pointer. I suspect that it frees the memory pointed to by that pointer before returning, but I haven't disassembled it yet.</p>
  
  <p>This only appears to happen when the wave playback system is low on buffers, which is why I'm using a single header to reproduce this.</p>
  
  <p>At this point I have a pretty good case against this being a bug in my application - after all, my application is not even running. Has anyone seen this before?</p>
  
  <p>I'm seeing this on Windows XP SP2. The audio card is from SigmaTel, and the driver version is 5.10.0.4995.</p>
  
  <p>Notes:</p>
  
  <p>To prevent confusion in the future, I'd like to point out that the answer suggesting that the problem lies with the use of malloc()/free() to manage the buffers being played is simply wrong. You'll note that I changed the code above to reflect the suggestion, to prevent more people from making the same mistake - it doesn't make a difference. The buffer being freed by waveCompleteHeader() is not the one containing the PCM data, the responsibility to free the PCM buffer lies with the application, and there's no requirement that it be allocated in any specific way.</p>
  
  <p>Also, I make sure that none of the waveOut API calls I use fail.</p>
  
  <p>I'm currently assuming that this is either a bug in Windows, or in the audio driver. Dissenting opinions are always welcome.</p>