IXAudio2* audio

Started by
19 comments, last by Reegan 15 years, 1 month ago
How come this doesnt work? winmain: --- IXAudio2* audio = 0; releaseAudio( &audio ); --- void releaseAudio( IXAudio2* audio ) { if( audio ) { audio->Release(); audio = 0; } } Its 3am here, what am I missing?
Advertisement
By "doesn't work", I'm assuming you get compile errors. The reason is in your WinMain, you're trying to pass a pointer to a pointer to an IXAudio2. Presumably, you want something like:

void releaseAudio(IXAudio2 *&audio){    if (audio)    {        audio->Release();        audio = 0;    }}....IXAudio2 *audio = 0;releaseAudio(audio);
You could also use a pointer to a pointer in order to use it like his origenal method showed. Personaly I prefer doing it this way since its clearer looking at the code that it may change the pointer since your passing a pointer to it.
void releaseAudio(IXAudio2** audio){    if(*audio)    {        (**audio).Release();        *audio = NULL;//set the pointer that we are pointing to to null    }}...IXAudio2 *audio = pointer to audio object...releaseAudio(&audio);//audio is now realeased and set to null
Or even simpler:
void releaseAudio( IXAudio2 *& audio ){    if (audio)    {        audio->Release();        audio = 0;    }}IXAudio2 * audio = ...;releaseAudio( audio );
And that is diffrent from the second post how exactly, cause I just looked at both 3 times and they both seem to be passing a refrence to a pointer.
Oh my bad, my page down skipped a post :)
Both have the same effect but the *& version has the disadvantage that it is less obvious that the releaseAudio function takes its parameters by reference and not by value. Thus making it hard to grasp at the first glance what happens when you look at the code. ;)
------------------------------------I always enjoy being rated up by you ...
Sorry about that, it was late:

audio.h(9) : error C2061: syntax error : identifier 'IXAudio2'
audio.h(11) : error C2065: 'IXAudio2' : undeclared identifier
audio.h(11) : error C2065: 'audio' : undeclared identifier

edit:
eh, im still getting the same errors..

---winmain, main.cpp---
IXAudio2* audio = 0;
releaseAudio( audio );
---audio.cpp---
void releaseAudio( IXAudio2 *& audio )
{
if( audio )
{
audio->Release();
audio = 0;
}
}
---audio.h
void releaseAudio( IXAudio2 *& audio );
Quote:Original post by 1st_maza
audio.h(9) : error C2061: syntax error : identifier 'IXAudio2'
audio.h(11) : error C2065: 'IXAudio2' : undeclared identifier
audio.h(11) : error C2065: 'audio' : undeclared identifier
Looks like you didn't #include <XAudio2.h>

By the way, the reason I chose the reference-to-pointer is so that the "releaseAudio" function looks like those "SAFE_RELEASE" macros that people tend to write (which is what I assumed this function was supposed to be a replacement for). Personally, I think writing the Release() logic out manually is clearer anyway (and you don't typically write it like that often either), but that's just personal preference.
"By the way, the reason I chose the reference-to-pointer is so that the "releaseAudio" function looks like those "SAFE_RELEASE" macros that people tend to write (which is what I assumed this function was supposed to be a replacement for). Personally, I think writing the Release() logic out manually is clearer anyway (and you don't typically write it like that often either), but that's just personal preference."

Well, I havent mastered all the correct ways of writing code, but im learning.

edit:
fatal error C1083: Cannot open include file: 'XAudio2.h': No such file or directory
This isnt included in SDK or when Visual is installed?

This topic is closed to new replies.

Advertisement