0
0

Hi, how are you.

I want ask one question about Fmod Callbacks, FMOD_Channel_SetUserData and memory leak

First, I put two pieces of code:

FMOD CALLBACK:

[code:4rj9ptq5]FMOD_RESULT end_sound( FMOD_CHANNEL *channel,
FMOD_CHANNEL_CALLBACKTYPE FMOD_CHANNEL_CALLBACKTYPE_END,
void *data1, void *data2)
{
dlsystem dsystem;

    LOG("ENTER CALLBACK FUNCTION", NULL);
    FMOD_Channel_GetUserData(channel, (void**)&dsystem);
    playlist_next(dsystem);
    LOG("EXIT CALLBACK", NULL);
    return FMOD_OK;

}[/code:4rj9ptq5]

And function that set a callback:

[code:4rj9ptq5]int playlist_next(dlsystem dsystem)
{
unsigned int id;
char url[64];
static int status = 0;
FMOD_RESULT ret;

    if(dsystem->dsound->sound != NULL) {
        LOG("CALLBACK OFF", NULL);
        FMOD_Channel_SetCallback(dsystem->dsound->channel, NULL);
        FMOD_Sound_Release(dsystem->dsound->sound);
    }

    while(ret != FMOD_OK) {
        if(dsystem->dsound->id != 0) {
            free_sound(dsystem->dsound);
        }
        LOG("EXTRACT DATA", NULL);    
        sql_extract_data(dsystem);
        LOG("DATA EXTRACTED", NULL);
        snprintf(url, sizeof(url)-1, "http://www.devicelost.com/mp3/0/%d.mp3", dsystem->dsound->id);
        ret = FMOD_System_CreateStream(dsystem->sound_system, url, FMOD_SOFTWARE | FMOD_2D, 0 ,&dsystem->dsound->sound);
        if(ret != FMOD_OK)
            fprintf(stderr, "Create stream: %s\n", FMOD_ErrorString(ret));
    }
    LOG("CREATE STREAM", NULL);

    ret = FMOD_System_PlaySound(dsystem->sound_system, FMOD_CHANNEL_FREE, dsystem->dsound->sound, 0, &dsystem->dsound->channel);
    if( ret != FMOD_OK ) {
        LOG("FMOD ERROR:", FMOD_ErrorString(ret));
        return 1;
    }

    LOG("PLAYING SOUND", NULL);
    FMOD_Channel_SetUserData(dsystem->dsound->channel, dsystem);  
    FMOD_Channel_SetCallback(dsystem->dsound->channel, (FMOD_CHANNEL_CALLBACK)end_sound);
    LOG("CALLBACK ON", NULL);

    return 0;

}[/code:4rj9ptq5]

Well, now I explain my doubt:

I have a gui with a button. When I click this button, the program calls the [code:4rj9ptq5]playlist_next[/code:4rj9ptq5] function. This, retrieves an id from mysql database, create a stream, plays sound, sets a user data for callback, and finally, sets callback.
If the sound stops, I enter to this function again, but this time, I disconnect the callback:

[code:4rj9ptq5]if(dsystem->dsound->sound != NULL) {
LOG("CALLBACK OFF", NULL);
FMOD_Channel_SetCallback(dsystem->dsound->channel, NULL);
FMOD_Sound_Release(dsystem->dsound->sound);
}[/code:4rj9ptq5]

If i doesn’t this, function enters in a loop.

My question is, every time that I do this:

[code:4rj9ptq5]FMOD_Channel_SetUserData(dsystem->dsound->channel, dsystem);[/code:4rj9ptq5] I’m doing a memory leak?

And my other question:

In callback function, I get data:

[code:4rj9ptq5]
dlsystem dsystem;
FMOD_Channel_GetUserData(channel, (void**)&dsystem);[/code:4rj9ptq5]

I supose that this function, internally allocates memory for dsystem and copy the content of the structure that i put with SetUserData to it. If this is true, every time that i call this function i do a memory leak, it’s true?

I say this, because i have a problem with a segfault. I listen 5 songs, and then, program crashes with segmentation fault:

I made a little debug, my log is:

The log of fifth song:

[code:4rj9ptq5]ENTER CALLBACK FUNCTION –
CALLBACK OFF –
Release dlsound –
EXTRACT DATA –
DATA EXTRACTED –
CREATE STREAM –
PLAYING SOUND –
CALLBACK ON –
EXIT CALLBACK –
segmentation fault[/code:4rj9ptq5]

Can anyone help me?

Thanks a lot

  • You must to post comments
0
0

Hi!

I’ve been thinking about it, but i don’t see the light.
The only thing that come to my mind, is that when i set the user data:

[code:3e0h3vr9]FMOD_Channel_SetUserData(dsystem->dsound->channel, dsystem);[/code:3e0h3vr9]

if i free FMOD::sound, the channel dissapears causing that my data is pointing to another site. Sincerely I’m not sure where is the problem.

Thanks

  • You must to post comments
0
0

Hi Peter, thanks to answer my question.

I explain this:

[code:3s3ep4po]FMOD_RESULT end_sound( FMOD_CHANNEL *channel,
FMOD_CHANNEL_CALLBACKTYPE FMOD_CHANNEL_CALLBACKTYPE_END,
void *data1, void *data2)[/code:3s3ep4po]

With this code, I want declare a callback to when the sounds stop, is this why I use [b:3s3ep4po]FMOD_CHANNEL_CALLBACKTYPE FMOD_CHANNEL_CALLBACKTYPE_END[/b:3s3ep4po]. Maybe i’m wrong, and this is not the way.

The other problem, i don’t understand it very well.
The main problem is that i call the [b:3s3ep4po]playlist_next[/b:3s3ep4po] function from inside the callback function, and then i free sound? I think that shouldn’t be a problem, no? Sorry, but i doesn’t understand it because it works four times, and in the fifth, it crash.

Thanks anyway

  • You must to post comments
0
0

I can see a few issues with the code you posted.

[code:n9rdzsa8]FMOD_RESULT end_sound( FMOD_CHANNEL *channel,
FMOD_CHANNEL_CALLBACKTYPE FMOD_CHANNEL_CALLBACKTYPE_END,
void *data1, void *data2)[/code:n9rdzsa8]

I’m not sure what this code is supposed to do but this line:
[code:n9rdzsa8]FMOD_CHANNEL_CALLBACKTYPE FMOD_CHANNEL_CALLBACKTYPE_END[/code:n9rdzsa8]
is declaring a variable with the name ‘FMOD_CHANNEL_CALLBACKTYPE_END’ which I suspect is not the intention. You should be comparing the value of that variable against FMOD_CHANNEL_CALLBACKTYPE_END.

Another thing is you are calling ‘playlist_next’ from inside the channel callback which, this will free the FMOD::Sound, you are not allowed to free the sound inside the channel callback. It may be convenient to do it that way but FMOD isn’t finished with the sound at that point.

  • You must to post comments
Showing 3 results
Your Answer

Please first to submit.