Skip to content

Commit

Permalink
8267819: CoInitialize/CoUninitialize should be called on same thread
Browse files Browse the repository at this point in the history
Reviewed-by: kcr, arapte
  • Loading branch information
Alexander Matveev committed Jun 3, 2021
1 parent 526f990 commit 47700d8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ void ReleaseNotificator(void *pObject) {
bool GSTDirectSoundNotify::Init(GSTDSNotfierCallback pCallback, void *pData) {
m_pCallback = pCallback;
m_pData = pData;
bool bCallCoUninitialize = true;
bool bResult = false;

if (FAILED(CoInitialize(NULL))) {
bCallCoUninitialize = false;
}

HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
NULL,
Expand All @@ -59,11 +65,15 @@ bool GSTDirectSoundNotify::Init(GSTDSNotfierCallback pCallback, void *pData) {
if (SUCCEEDED(hr)) {
hr = m_pEnumerator->RegisterEndpointNotificationCallback(this);
if (SUCCEEDED(hr)) {
return true;
bResult = true;
}
}

return false;
if (bCallCoUninitialize) {
CoUninitialize();
}

return bResult;
}

void GSTDirectSoundNotify::Dispose() {
Expand All @@ -78,13 +88,6 @@ GSTDirectSoundNotify::GSTDirectSoundNotify() {
m_pEnumerator = NULL;
m_pCallback = NULL;
m_pData = NULL;
m_hrCoInit = CoInitialize(NULL);
}

GSTDirectSoundNotify::~GSTDirectSoundNotify() {
if (SUCCEEDED(m_hrCoInit)) {
CoUninitialize();
}
}

HRESULT GSTDirectSoundNotify::OnDefaultDeviceChanged(EDataFlow flow,
Expand Down Expand Up @@ -112,7 +115,7 @@ HRESULT GSTDirectSoundNotify::QueryInterface(REFIID iid, void** ppUnk) {

AddRef();

return S_OK;
return S_OK;
}

ULONG GSTDirectSoundNotify::AddRef() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class GSTDirectSoundNotify : IMMNotificationClient
{
public:
GSTDirectSoundNotify();
~GSTDirectSoundNotify();

bool Init(GSTDSNotfierCallback pCallback, void *pData);
void Dispose();
Expand All @@ -67,7 +66,6 @@ class GSTDirectSoundNotify : IMMNotificationClient
IMMDeviceEnumerator* m_pEnumerator;
GSTDSNotfierCallback m_pCallback;
void *m_pData;
HRESULT m_hrCoInit;

// IUnknown
IFACEMETHODIMP QueryInterface(const IID& iid, void** ppUnk);
Expand Down

1 comment on commit 47700d8

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.