IXAudio2MasteringVoice error when enclosed in a ComPtr : 'Release': is not a member of 'IXAudio2MasteringVoice'

KeyC0de 31 Reputation points
2020-10-21T13:52:10.857+00:00

I'm following XAudio2 tutorial from Microsoft: https://video2.skills-academy.com/en-us/windows/win32/xaudio2/how-to--initialize-xaudio2

I'm on Windows 8.1 x64, Visual Studio 2017.

This is my code:

#include "com_initializer.h"  
#include <xaudio2.h>  
#include <wrl/client.h>  
  
#pragma comment( lib, "xaudio2_8.lib" )  
  
namespace mwrl = Microsoft::WRL;  
  
int main()  
{  
	/// 1. Initialize COM  
	COMInitializer comInitializer;  
  
	/// 2. Create XAudio2 engine  
	mwrl::ComPtr<IXAudio2> pXaudio2;  
	HRESULT hres;  
	hres = XAudio2Create( &pXaudio2,  
		0u,  
		XAUDIO2_DEFAULT_PROCESSOR );  
	if ( FAILED( hres ) )  
	{  
		return hres;  
	}  
  
	/// 3. Create a mastering voice  
	mwrl::ComPtr<IXAudio2MasteringVoice> pMasterVoice;  
	hres = pXaudio2->CreateMasteringVoice( &pMasterVoice );  
	if ( FAILED( hres ) )  
	{  
		return hres;  
	}  
  
  
}  

I get the following error:
Error C2039 - 'Release': is not a member of 'IXAudio2MasteringVoice'

It works fine if I replace the com pointers with raw pointers.

XAudio2 doesn't work with ComPtr s or what?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,512 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,628 questions
0 comments No comments
{count} votes

Accepted answer
  1. Drake Wu - MSFT 991 Reputation points
    2020-10-22T03:34:49.797+00:00

    Hi, @KeyC0de ComPtr will automatically call the Release function after the pointer's life cycle ends:

        ~ComPtr() throw()  
        {  
            InternalRelease();  
        }  
        unsigned long InternalRelease() throw()  
        {  
            unsigned long ref = 0;  
            T* temp = ptr_;  
      
            if (temp != nullptr)  
            {  
                ptr_ = nullptr;  
                ref = temp->Release();  
            }  
      
            return ref;  
        }  
    

    IXAudio2MasteringVoice inherits from IXAudio2Voice, neither of which has a release method. So you got that error. The same as IXAudio2SourceVoice and IXAudio2SubmixVoice. Use them as @Castorix31 said:

    IXAudio2Voice* pVoice;  
    IXAudio2MasteringVoice* pMasterVoice;  
    IXAudio2SourceVoice* pSourceVoice;  
    IXAudio2SubmixVoice* pSubmixVoice;  
    

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Castorix31 83,101 Reputation points
    2020-10-21T19:57:48.64+00:00

    XAudio2 doesn't work with ComPtr s or what?

    For IXAudio2, yes, but for IXAudio2MasteringVoice you must use

     IXAudio2MasteringVoice* pMasterVoice;
    

    like in MS sample : Audio.h

    0 comments No comments