Sign in to follow this  
pbjgame

DirectSound delete sound function

Recommended Posts

if (sound_nameofsound != NULL) delete sound_nameofsound; is causing a "program must close" "send error report" thing when I escape the program. anyone know a better way to clear directx sounds from system memory, or does it auto-clear?

Share this post


Link to post
Share on other sites
What type is "sound_nameofsound"? If it's a COM interface pointer (all of the objects you use with DSound are COM objects), then you can't just delete it. The lifetime of a COM object is maintained by calling AddRef and Release, which increment or decrement the object's internal reference count. The typical way for something to say "I'm done with this interface" is to call Release on it, that way when everyone is done the object deletes itself.

If you're not familiar with any of this, you should have a look at the COM Overview found in the DX SDK.

Share this post


Link to post
Share on other sites
is sound_nameofsound a direct sound buffer?

if it is, you should be calling ->Release() on it rather than using delete.

my knowledge of direct sound is pretty much non existant so if i'm wrong.. I dunno! :)

Share this post


Link to post
Share on other sites
I tried the release function, and the sound name is "sound_nameofsound".

Release would be
//...
if (sound_nameofsound != NULL)
sound_nameofsound->Release();
//...
and that was generating a compiling error.

Share this post


Link to post
Share on other sites
Quote:
Original post by pbjgame
I tried the release function, and the sound name is "sound_nameofsound".

Release would be
//...
if (sound_nameofsound != NULL)
sound_nameofsound->Release();
//...
and that was generating a compiling error.
Telling us what the compile error was would be helpful...

And again, what is the type of sound_nameofsound? Can we see it's declaration?

Share this post


Link to post
Share on other sites
CSound *sound_bounce; //variable declaration.
//...
sound_bounce = LoadSound("bounce.wav"); //loadsound function
if (sound_bounce == NULL)
return 0;
//...
if (sound_bounce != NULL)
sound_bounce->Release();
//...

260 C:\...\My Projects\dxaudio\game.cpp 'class CSound' has no member named 'Release'

and when I use
if (sound_bounce != NULL)
delete sound_bounce;
it gives me one of those "send message" "don't send message" error boxes, and when I use no delete function it seems to work just fine.

Share this post


Link to post
Share on other sites
Possibly the CSound class from the DX SDK?


class CSound
{
protected:
LPDIRECTSOUNDBUFFER* m_apDSBuffer;
DWORD m_dwDSBufferSize;
CWaveFile* m_pWaveFile;
DWORD m_dwNumBuffers;
DWORD m_dwCreationFlags;

HRESULT RestoreBuffer( LPDIRECTSOUNDBUFFER pDSB, BOOL* pbWasRestored );

public:
CSound( LPDIRECTSOUNDBUFFER* apDSBuffer, DWORD dwDSBufferSize, DWORD dwNumBuffers, CWaveFile* pWaveFile, DWORD dwCreationFlags );
virtual ~CSound();

HRESULT Get3DBufferInterface( DWORD dwIndex, LPDIRECTSOUND3DBUFFER* ppDS3DBuffer );
HRESULT FillBufferWithSound( LPDIRECTSOUNDBUFFER pDSB, BOOL bRepeatWavIfBufferLarger );
LPDIRECTSOUNDBUFFER GetFreeBuffer();
LPDIRECTSOUNDBUFFER GetBuffer( DWORD dwIndex );

HRESULT Play( DWORD dwPriority = 0, DWORD dwFlags = 0, LONG lVolume = 0, LONG lFrequency = -1, LONG lPan = 0 );
HRESULT Play3D( LPDS3DBUFFER p3DBuffer, DWORD dwPriority = 0, DWORD dwFlags = 0, LONG lFrequency = 0 );
HRESULT Stop();
HRESULT Reset();
BOOL IsSoundPlaying();
};

Share this post


Link to post
Share on other sites
Quote:
Original post by pbjgame
oui, monsieur lemon head.

any idea how to delete the sound on game_end? or is it automatic?
It depends how you created it.

Run your app in the debugger, running it like you are is like writing source code and never compiling it and then sending it off as your game - you have absolutely no idea it it'll work as expected.
There's a reason that companies spend a crapload of money to develop debuggers, they're the single most useful tool you can ever use when programming.

Share this post


Link to post
Share on other sites
//I think this is what you were asking for, then? Sorry, I was confused.

#include "dxaudio.h"

CSoundManager *dsound;

int Init_DirectSound(HWND hwnd)
{
HRESULT result;

//create DirectSound manager object
dsound = new CSoundManager();

//initialize DirectSound
result = dsound->Initialize(hwnd, DSSCL_PRIORITY);
if (result != DS_OK)
return 0;

//set the primary buffer format
result = dsound->SetPrimaryBufferFormat(2, 22050, 16);
if (result != DS_OK)
return 0;

//return success
return 1;
}

CSound *LoadSound(char *filename)
{
HRESULT result;

//create local reference to wave data
CSound *wave;

//attempt to load the wave file
result = dsound->Create(&wave, filename);
if (result != DS_OK)
return NULL;

//return the wave
return wave;
}

Share this post


Link to post
Share on other sites
It appears that you are using the CSoundManager and CSound from the SDK. You need a CSound destructor something like the following:

CSound::~CSound()
{
for( DWORD i=0; i<m_dwNumBuffers; i++ )
{
SAFE_RELEASE( m_apDSBuffer[i] );
}

SAFE_DELETE( m_apDSBuffer );
SAFE_DELETE( m_pWaveFile );
}

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