Clean up refcounts of COM objects.

pull/881/head
Mooneer Salem 2025-05-13 13:57:45 -07:00
parent 1c402df66f
commit f388b982f6
2 changed files with 30 additions and 3 deletions

View File

@ -50,7 +50,7 @@ WASAPIAudioDevice::WASAPIAudioDevice(IAudioClient* client, IAudioEngine::AudioDi
, isRenderCaptureRunning_(false)
, semaphore_(nullptr)
{
// empty
client_->AddRef();
}
WASAPIAudioDevice::~WASAPIAudioDevice()
@ -299,6 +299,18 @@ void WASAPIAudioDevice::start()
log_warn("Could not initialize COM (res = %d)", res);
}
// Increment refcounts of COM objects used by thread
// to avoid instability during stop/restart.
client_->AddRef();
if (renderClient_ != nullptr)
{
renderClient_->AddRef();
}
if (captureClient_ != nullptr)
{
captureClient_->AddRef();
}
// Temporarily raise priority of task
setHelperRealTime();
@ -321,6 +333,18 @@ void WASAPIAudioDevice::start()
log_info("Exiting render/capture thread");
clearHelperRealTime();
// Decrement refcounts prior to exit.
client_->Release();
if (renderClient_ != nullptr)
{
renderClient_->Release();
}
if (captureClient_ != nullptr)
{
captureClient_->Release();
}
CoUninitialize();
});

View File

@ -303,6 +303,9 @@ std::shared_ptr<IAudioDevice> WASAPIAudioEngine::getAudioDevice(wxString deviceN
auto devPtr = new WASAPIAudioDevice(client, direction, sampleRate, numChannels);
result = std::shared_ptr<IAudioDevice>(devPtr);
client->Release();
device->Release();
}
}
prom->set_value(result);