IDirectMusic8 crash my program!!! why?

Started by
5 comments, last by Ilankt 20 years, 5 months ago
here is the code (it''s from 2kings a bit modified) when I run the program at the first time, all is good, I can hear the sound file but when I quit the program and run it again, the computer crash, and I need to restart computer... here is the code, a bit long, but please help me!

class CSound
{
	public:
		void Load(CAudio Audio,char *file);
		void Play();
		void Stop();
		bool IsPlaying();
		void ShutDownSound();
	private:
		IDirectMusicPerformance8*		Performance;
		IDirectMusicLoader8*		Loader;
		IDirectMusicSegment8*		Segment;
};

void CSound::Load(CAudio Audio,char *file)
{
	WCHAR wcFile[MAX_PATH];
	Segment=NULL;
	Performance=Audio.GetPerformance();
	Loader=Audio.GetLoader();
	CoCreateInstance(CLSID_DirectMusicSegment,NULL,CLSCTX_INPROC,IID_IDirectMusicSegment8,(void**)&Segment);
	MultiByteToWideChar(CP_ACP,0,file,-1,wcFile,MAX_PATH);
	Loader->LoadObjectFromFile(CLSID_DirectMusicSegment,IID_IDirectMusicSegment8,wcFile,(void**)&Segment);
	Segment->Download(Performance);
}
void CSound::Play()
{
	Performance->PlaySegmentEx(Segment,0,NULL,DMUS_SEGF_SECONDARY,0,0,NULL,NULL);
}
void CSound::Stop()
{
	Performance->StopEx(Segment,0,0);
}
bool CSound::IsPlaying()
{
	return (Performance->IsPlaying(Segment,NULL) == S_OK);
}

void CSound::ShutDownSound()
{
	Segment->Unload(Performance);
	Segment->Release();
	CoUninitialize();
}

class CAudio
{
	public:
		void InitAudio();
		void SetFilesDirectory();
		void ShutDownAutdio();
		void SetMasterVolume(long volume);
		inline IDirectMusicPerformance8* GetPerformance()
		{
			return Performance;
		}	
		inline IDirectMusicLoader8* GetLoader()
		{
			return Loader;
		}
	private:
		IDirectMusicPerformance8 *Performance;
		IDirectMusicLoader8 *Loader;
};

void CAudio::InitAudio()
{
	CoInitialize(NULL);
	CoCreateInstance(CLSID_DirectMusicLoader,NULL,CLSCTX_INPROC,IID_IDirectMusicLoader8,(void**)&Loader);
	CoCreateInstance(CLSID_DirectMusicPerformance,NULL,CLSCTX_INPROC,IID_IDirectMusicPerformance8,(void**)&Performance);
	Performance->InitAudio(NULL,NULL,NULL,DMUS_APATH_DYNAMIC_STEREO,64,DMUS_AUDIOF_ALL,NULL);			
	SetFilesDirectory();
}
void CAudio::SetFilesDirectory()
{
	char szSearchPath[MAX_PATH];

	GetCurrentDirectory(MAX_PATH,szSearchPath);
	strcat(szSearchPath,"//sounds");

	WCHAR wszSearchPath[MAX_PATH];
	MultiByteToWideChar(CP_ACP,0,szSearchPath,-1,wszSearchPath,MAX_PATH);

	Loader->SetSearchDirectory(GUID_DirectMusicAllTypes,wszSearchPath,false);
}

void CAudio::ShutDownAutdio()
{
	Performance->Stop(NULL,NULL,0,0);
	Loader->Release();
	Performance->CloseDown();
	Performance->Release();
	CoUninitialize();
}

void CAudio::SetMasterVolume(long volume)
{
	Performance->SetGlobalParam(GUID_PerfMasterVolume,&volume,sizeof(long));
}
How appropriate, you fight like a cow!
Advertisement
can you help me please???

I tried to figure what is the problem but I dont know why this happens...

I think the problem is that I''m not releasing them good, but I call the ShutDownSound() and ShutAudio()
How appropriate, you fight like a cow!
im totally new to programing so i dont know anything yet, but when i ws looking through the code box thigies, I went to the last one and saw this bit

strcat(szSearchPath,"//sounds");


//means comment and so the closing of the " is not closed, its part of the comment, and so the rest of the code from then on is just part of a string.

Probably got nothing todo with that, but i saw that as i was scrolling past :/

Get rid of the CoUninitialize call in the CSound class as it never calls CoInitialize(Ex). That''s one error I can spot that might possibly lead to that kind of behavior.
.
Mastaba:thanks, I did but the problem is not in CSound, it''s in CAudio...
How appropriate, you fight like a cow!
quote:Original post by Pred
im totally new to programing so i dont know anything yet, but when i ws looking through the code box thigies, I went to the last one and saw this bit

strcat(szSearchPath,"//sounds");


//means comment and so the closing of the " is not closed, its part of the comment, and so the rest of the code from then on is just part of a string.

Probably got nothing todo with that, but i saw that as i was scrolling past :/



I think the quotes have higher priority than the slashes (comments).

--
You''re Welcome,
Rick Wong
- sitting in his chair doing the most time-consuming thing..
quote:Original post by Ilankt
Mastaba:thanks, I did but the problem is not in CSound, it''s in CAudio...


But, having CoUninitialize in CSound was a problem none the less. One that would screw up the CAudio shutdown since the directmusic dll''s would get unloaded prematurely.
.

This topic is closed to new replies.

Advertisement