In practice to control releasing instances of the COM objects in the application you can use the helper template classes such as
CComPtr
CComQIPtr
or
CInterfaceList
of the ATL. That allowing you to work with COM interfaces as with the regular C++ classes without afraiding that any object will not be freed. See an example:
CComPtr<IMMDeviceEnumerator> _enum;
_enum.CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL);
if (_enum) {
CComPtr<IMMDevice> _device;
_enum->GetDefaultAudioEndpoint(eRender, eConsole, &_device);
CComQIPtr<IAudioClient> _client = _device;
if (_client) {
}
_enum.Release();
CComPtr<IMMDevice> _device2 = _device;
}
In some cases I create proxy one or few interfaces of the object and pass then instead to track nested object releasing and to see what interfaces queried from it and so on, but this may not be necessary in your case.
Regards,
Maxim