Sign in to follow this  
1st_maza

IXAudio2* audio

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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


Share this post


Link to post
Share on other sites
Or even simpler:
void releaseAudio( IXAudio2 *& audio )
{
if (audio)
{
audio->Release();
audio = 0;
}
}

IXAudio2 * audio = ...;
releaseAudio( audio );

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. ;)

Share this post


Link to post
Share on other sites
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 );

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
"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?

Share this post


Link to post
Share on other sites
Quote:
Original post by 1st_maza
"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?
No, you need the latest DirectX SDK.

Share this post


Link to post
Share on other sites
Quote:
Original post by 1st_maza
That is weird, I just downloaded SDK from Microsofts website.. are there more than one?
I believe only the latest is downloadable. However, sometimes it doesn't set up the SDK paths when updating (E.g. if you have visual studio open during the install). In which case you'll need to add the SDK paths yourself.

Share this post


Link to post
Share on other sites
Well, the link you've stated takes me to another SDK that I havent installed, the other one was for directx and alot smaller.

Share this post


Link to post
Share on other sites
Quote:
Original post by 1st_maza
Well, the link you've stated takes me to another SDK that I havent installed, the other one was for directx and alot smaller.
There's the DirectX End-User Runtime, but that's not a SDK - that's just the DirectX DLLs. If you want to develop software using DirectX you'll need the SDK. The current SDK weighs in at 483.2 MB.

Share this post


Link to post
Share on other sites
Quote:
Original post by 1st_maza
Yeah, im downloading the 2nd.. should I remove the dx version before installing the other one?
If you have an older version of the SDK, yes, I'd remove that first. As for the runtime, you can't uninstall that - only upgrade it (Or roll back using system restore).

Share this post


Link to post
Share on other sites
After installing that SDK my visual got nuts when I try to debug.. I have like 25 times more errors or warnings of things Ive never had problem with before..

edit:
ignore this post..

[Edited by - 1st_maza on March 19, 2009 6:35:48 PM]

Share this post


Link to post
Share on other sites
OK, but you have to tell us what the errors and warnings are before we can begin to try and help you...

Share this post


Link to post
Share on other sites
Even with:

#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <xaudio2.h>
#include "graphic.h"
#include "audio.h"

#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
#pragma comment(lib, "xaudio2.lib")

I get:

audio.cpp(13) : fatal error C1083: Cannot open include file: 'xaudio2.h': No such file or directory
graphic.cpp(9) : fatal error C1083: Cannot open include file: 'd3d9.h': No such file or directory
main.cpp(9) : fatal error C1083: Cannot open include file: 'd3d9.h': No such file or directory

All three files have the pragma comments..

Share this post


Link to post
Share on other sites
Huh, whats this about pointers to pointers?
I would have thought all he/she needed to do was take off the '&' operator where he/she calls

releaseAudio( &audio );

so it would be like this:

releaseAudio( audio );

am i missing something here?
From what i know, thats the only problem with that code, providing the code around what is shown is ok also.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this