我正在使用适用于Windows 8.1商店的Windows音频会话(WASAPI)示例的音频捕获部分,上次更新时间是2015年3月24日。我写作是因为我可以看到看似错误或过于复杂的代码示例。我猜这是因为我不完全理解WASAPI界面和Windows运行时工作队列,所以我希望Stack Overflow社区可以告诉我为什么这不是' ' ta bug。
所有这些都在WASAPICapture.cpp中。现在,我将用最简单(也是最严重)的例子测试水域:将指针用于已释放的缓冲区。
以下是一些突出显示特定行的伪代码。行号与更完整的代码提取相对较低。
P.S。 - 我对互联网的访问是零星的,而且我是新手 - 所以请耐心等待我的回复。
Line 1: hr = m_AudioCaptureClient->GetBuffer( &Data, &FramesAvailable, ...
... returns in Data a pointer to the audio buffer
Line 21: auto dataByte = ref new Platform::Array<BYTE, 1>( Data, cbBytesToCapture );
... copies from the buffer into a new Platform Array for the GUI's 'scope.
Line 24: m_AudioCaptureClient->ReleaseBuffer( FramesAvailable );
... releases the buffer
Line 27: ProcessScopeData( Data, cbBytesToCapture );
... uses the pointer after the underlying buffer has been released!?
...这里是更完整的代码摘录:
hr = m_AudioCaptureClient->GetBuffer( &Data, &FramesAvailable, &dwCaptureFlags, &u64DevicePosition, &u64QPCPosition );
if (FAILED( hr ))
{
goto exit;
}
if (dwCaptureFlags & AUDCLNT_BUFFERFLAGS_DATA_DISCONTINUITY)
{
// Pass down a discontinuity flag in case the app is interested and reset back to capturing
m_DeviceStateChanged->SetState( DeviceState::DeviceStateDiscontinuity, S_OK, true );
m_DeviceStateChanged->SetState( DeviceState::DeviceStateCapturing, S_OK, false );
}
// Zero out sample if silence
if ( (dwCaptureFlags & AUDCLNT_BUFFERFLAGS_SILENT) || IsSilence )
{
memset( Data, 0, FramesAvailable * m_MixFormat->nBlockAlign );
}
// Store data in array
auto dataByte = ref new Platform::Array<BYTE, 1>( Data, cbBytesToCapture );
// Release buffer back
m_AudioCaptureClient->ReleaseBuffer( FramesAvailable );
// Update plotter data
ProcessScopeData( Data, cbBytesToCapture );
// Write File and async store
m_WAVDataWriter->WriteBytes( dataByte );
答案 0 :(得分:1)
你是对的。
这里有两个问题。