Sign in to follow this  
Marktheguy

Stack overflow with fmodupdate in a thread

Recommended Posts

I have created a thread to update fmod
void ThreadProc(void *param)
{
	while(threadactive==1){
		FMOD_System_Update(fmodsystem);
		Sleep(100);
	}
	_endthread();
}
[...]
handle = (HANDLE) _beginthread( ThreadProc,0,NULL);
[...]
I'm playing a wav with a syncpoint, when the syncpoint is reached my callback gets called.
FMOD_RESULT F_CALLBACK channelcallback(FMOD_CHANNEL *  channel, FMOD_CHANNEL_CALLBACKTYPE  type, unsigned int  commanddata1, unsigned int commanddata2)
{
    if(tempswitch==1){ 
        result = FMOD_Channel_Stop(channel);
        ERRCHECK(result);
        tempswitch=0;	
    }
    return FMOD_OK;
}
The program crashes on FMOD_Channel_Stop in the callback. "Stack overflow". It ONLY happends when the callback gets called by reaching the syncpoint. Never if I do anything else. Any thoughts on how to solve it? I really need having the fmodupdate in a seperate thread. Thanks [Edited by - Marktheguy on December 17, 2008 7:24:36 AM]

Share this post


Link to post
Share on other sites
What's almost certainly going on is that the FMOD_Channel_Stop() call is internally calling that callback, getting you into an infinite recursive loop. A quick look at the call stack in the debugger will tell you for certain.

One solution is to not call FMOD_Channel_Stop() in the callback, but just set a flag (or add an entry to a list just in case you get lots of callbacks) that you act on elsewhere.

Share this post


Link to post
Share on other sites
The channel callback type parameter will also tell you why the callback was triggered. In particular, you will be getting the callback with type FMOD_CHANNEL_CALLBACKTYPE_SYNCPOINT, on which you will be stopping the channel thus triggering the callback with type FMOD_CHANNEL_CALLBACKTYPE_END, on which you stop the channel and the callback gets triggered with type FMOD_CHANNEL_CALLBACKTYPE_END and so on until death.

Also note that since you are calling the FMOD update in a thread, the callback will be called in the context of the thread. And make absolutely sure that all FMOD functions are called from the same thread. This is emphasized many times in the FMOD forums and documentation. If you are trying to have the FMOD update in a separate thread from the other calls to FMOD functions, you will have problems. Simple. The answer is to don't do it.

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