mackron / miniaudio

Audio playback and capture library written in C, in a single source file.
https://miniaud.io
Other
4.07k stars 361 forks source link

Crash #443

Closed OriginTwo closed 2 years ago

OriginTwo commented 2 years ago

Hello, I'm having an issue with loading ma_sound. The program always crash when I call 'ma_sound_init_from_file()'.

It usually crashes here: image

But sometimes, it crashes because of an assertion failure: image image

mackron commented 2 years ago

You need to post your initialization code.

OriginTwo commented 2 years ago

It seems that I've solved the problem.

I have a function that load the sound for me, it looks something like this:

ma_sound load_sound(ma_engine *engine, const char *filepath)
{
    ma_sound sound = {0};
    ma_result result = ma_sound_init_from_file(engine, filepath, flags, NULL, NULL, &sound);
    return sound;
}

What I did was change it to look something like this:

void load_sound(ma_sound *sound, ma_engine *engine, const char *filepath)
{
    ma_result result = ma_sound_init_from_file(engine, filepath, flags, NULL, NULL, sound);
}
mackron commented 2 years ago

Yep, the ma_sound structure is a transparent struct, not a handle. As soon as the function returns that object will end up getting destroyed. You need to manage the lifetime of the ma_sound object yourself.