mardi 30 décembre 2014

Crash when SAFE_RELEASE is called on IMMDeviceEnumerator

I am using a service to detect the connection of head set. When the microphone is connected and disconnected, I am getting notification. The thing is, when I end the service manually, I am facing a crash in the service while doing SAFE_RELEASE. Here is the code...



NotifyHandsetConnectionStatus::NotifyHandsetConnectionStatus() : _cRef(1), _pEnumerator(NULL){}

NotifyHandsetConnectionStatus::~NotifyHandsetConnectionStatus()
{
// SAFE_RELEASE(_pEnumerator)
if (_pEnumerator)
{
_pEnumerator->Release(); // CRASH
_pEnumerator = NULL;
}
}

void NotifyHandsetConnectionStatus::Init(DWORD threadID)
{
PTTThreadID = threadID;
HRESULT hr = S_OK;

CoInitialize(NULL);

if (_pEnumerator == NULL)
{
// Get enumerator for audio endpoint devices.
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER,
__uuidof(IMMDeviceEnumerator), (void**)&_pEnumerator);
}

if (hr == S_OK)
{
_pEnumerator->RegisterEndpointNotificationCallback(this);
}
}


and here is the class declaration...



class NotifyHandsetConnectionStatus : public IMMNotificationClient
{
LONG _cRef;
IMMDeviceEnumerator *_pEnumerator;
DWORD PTTThreadID;
public:
NotifyHandsetConnectionStatus();
~NotifyHandsetConnectionStatus();

void Init(DWORD threadID);
ULONG STDMETHODCALLTYPE AddRef() override;
virtual ULONG STDMETHODCALLTYPE Release() override;
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, VOID **ppvInterface) override;
HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(LPCWSTR pwstrDeviceId, DWORD dwNewState) override;
HRESULT STDMETHODCALLTYPE OnDeviceAdded(LPCWSTR pwstrDeviceId) override
{
return S_OK;
}
HRESULT STDMETHODCALLTYPE OnDeviceRemoved(LPCWSTR pwstrDeviceId) override
{
return S_OK;
}
HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(LPCWSTR pwstrDeviceId, const PROPERTYKEY key) override
{
return S_OK;
}
HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged( EDataFlow flow, ERole role, LPCWSTR pwstrDeviceId) override
{
return S_OK;
}
};

Aucun commentaire:

Enregistrer un commentaire