Sdl_mixer: am I changing music correctly?

I’m using sdl_mixer to play music and sound effects in my game. It seems to work
properly, but once in a while I get a crash report on my forum, which is related
to sdl_mixer playing music. So, I’m just wondering if my code is correct for
loading music and playing it:

PROCEDURE PlayMusicFile(Const MusicFile : PChar);
BEGIN
IF MusicSetting > 0 THEN BEGIN
IF NOT MusicFreed THEN Mix_FreeMusic(MusicNumber);
MusicNumber := Mix_LoadMUS(MusicFile);
MusicFreed := False;
Mix_PlayMusic(MusicNumber, 1);
END;
END;

The MusicFreed variable keeps track of whether or not there’s an existing piece
of music that should be freed from memory before loading the new music.
MusicSetting is just the volume that the player has set music to. I’m thinking
that maybe I have to Mix_HaltMusic before freeing it and loading another tune,
but the documentation says that Mix_FreeMusic automatically halts it.

So, can anybody see anything wrong? Something that may be the cause for these
random crashes?

PS: I’m pretty sure that these crashes are a result of the music, as the line
backtrace info in the error message shows the PlayMusicFile as the last
procedure loaded. It then crashes somewhere in the sdl_mixer code.

I’m using sdl_mixer to play music and sound effects in my game. It seems to work
properly, but once in a while I get a crash report on my forum, which is related
to sdl_mixer playing music. So, I’m just wondering if my code is correct for
loading music and playing it:

PROCEDURE PlayMusicFile(Const MusicFile : PChar);
BEGIN
IF MusicSetting > 0 THEN BEGIN
IF NOT MusicFreed THEN Mix_FreeMusic(MusicNumber);
MusicNumber := Mix_LoadMUS(MusicFile);
MusicFreed := False;
Mix_PlayMusic(MusicNumber, 1);
END;
END;

So, can anybody see anything wrong? Something that may be the cause for these
random crashes?

Well, you have a rather ugly habit of capitalizing keywords. If I was the program
I’d crash too, just to get away from that! :stuck_out_tongue:

PS: I’m pretty sure that these crashes are a result of the music, as the line
backtrace info in the error message shows the PlayMusicFile as the last
procedure loaded. It then crashes somewhere in the sdl_mixer code.

Hard to give you a good response from there. Your code looks good. Are you
able to reproduce the bug yourself, or is this purely a report? Delphi doesn’t crash;
it raises exceptions. What exception are you getting? It sounds like it’s coming
from withing SDL_Mixer. Do you have Visual Studio? If not, try downloading the
Express (free) version of VC++ and building the DLL in debug mode, then attaching
the debugger to your program. (Note: this won’t work if the Delphi debugger is
already attached to it.) Put a breakpoint on Mix_PlayMusic and start from there…>----- Original Message ----

From: Christian Knudsen
Subject: [SDL] sdl_mixer: am I changing music correctly?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello,

Don’t call Mix_ functions from within the music callback. I’ve had
issues with that in the past, but the piece of code you have is fine.
(not sure if you’re even using the callbacks though)

Edgar

Mason Wheeler wrote:
|> ----- Original Message ----
|
|> From: Christian Knudsen
|> Subject: [SDL] sdl_mixer: am I changing music correctly?
|>
|> I’m using sdl_mixer to play music and sound effects in my game. It
seems to work
|> properly, but once in a while I get a crash report on my forum, which
is related
|> to sdl_mixer playing music. So, I’m just wondering if my code is
correct for
|> loading music and playing it:
|>
|> PROCEDURE PlayMusicFile(Const MusicFile : PChar);
|> BEGIN
|> IF MusicSetting > 0 THEN BEGIN
|> IF NOT MusicFreed THEN Mix_FreeMusic(MusicNumber);
|> MusicNumber := Mix_LoadMUS(MusicFile);
|> MusicFreed := False;
|> Mix_PlayMusic(MusicNumber, 1);
|> END;
|> END;
|>
|> So, can anybody see anything wrong? Something that may be the cause
for these
|> random crashes?
|
| Well, you have a rather ugly habit of capitalizing keywords. If I was
the program
| I’d crash too, just to get away from that! :stuck_out_tongue:
|
|> PS: I’m pretty sure that these crashes are a result of the music, as
the line
|> backtrace info in the error message shows the PlayMusicFile as the last
|> procedure loaded. It then crashes somewhere in the sdl_mixer code.
|
| Hard to give you a good response from there. Your code looks good.
Are you
| able to reproduce the bug yourself, or is this purely a report?
Delphi doesn’t crash;
| it raises exceptions. What exception are you getting? It sounds like
it’s coming
| from withing SDL_Mixer. Do you have Visual Studio? If not, try
downloading the
| Express (free) version of VC++ and building the DLL in debug mode,
then attaching
| the debugger to your program. (Note: this won’t work if the Delphi
debugger is
| already attached to it.) Put a breakpoint on Mix_PlayMusic and start
from there…
|
| _______________________________________________
| SDL mailing list
| SDL at lists.libsdl.org
| http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAknp8mkACgkQolm4VNX3QTwhlwCeKjx11KuWEmVcNd5pWv/JyiTk
O4kAoJ5s59JGMWZmymotVwHbO90hqI9x
=zkym
-----END PGP SIGNATURE-----

Are you stopping playback before you free the old song to load the new?

-bill!On Sat, Apr 18, 2009 at 10:49:53AM +0000, Christian Knudsen wrote:

  IF NOT MusicFreed THEN Mix_FreeMusic(MusicNumber);

Bill Kendrick <nbs sonic.net> writes:

  IF NOT MusicFreed THEN Mix_FreeMusic(MusicNumber);

Are you stopping playback before you free the old song to load the new?

-bill!

No, I’m not, as the documentation says that using Mix_FreeMusic will halt the
music. I seem to remember that I had a Mix_HaltMusic in an earlier version to
see if that would do anything, but I still got a few crash reports.> On Sat, Apr 18, 2009 at 10:49:53AM +0000, Christian Knudsen wrote:

Edgar Simo <bobbens gmail.com> writes:

Hello,

Don’t call Mix_ functions from within the music callback. I’ve had
issues with that in the past, but the piece of code you have is fine.
(not sure if you’re even using the callbacks though)

Edgar

I’m not using callbacks. The game loop just checks every loop that the music is
still playing - if not, it calls this procedure.

Mason Wheeler <masonwheeler yahoo.com> writes:

Well, you have a rather ugly habit of capitalizing keywords. If I was the
program
I’d crash too, just to get away from that! :stuck_out_tongue:

Yeah, I know a lot of programmers balk at me capitalizing most things. I just
like the look of the code better. :slight_smile:

Hard to give you a good response from there. Your code looks good. Are you
able to reproduce the bug yourself, or is this purely a report? Delphi
doesn’t crash;
it raises exceptions. What exception are you getting? It sounds like it’s
coming
from withing SDL_Mixer. Do you have Visual Studio? If not, try downloading the
Express (free) version of VC++ and building the DLL in debug mode, then
attaching
the debugger to your program. (Note: this won’t work if the Delphi debugger is
already attached to it.) Put a breakpoint on Mix_PlayMusic and start from
there…

I’ll give it a shot, but the big problem is that I’m not really getting these
crashes myself, only some other players of the game. By the way, I’m not using
Delphi - I’m using the FreePascal IDE.

I’ll give it a shot, but the big problem is that I’m not really getting these
crashes myself, only some other players of the game.
OK, that’s your first problem. There’s really nothing you can do until
you’re able to reproduce the problem locally. Ask your users if they
can provide some more specific information.

By the way, I’m not using
Delphi - I’m using the FreePascal IDE.
shrug Close enough…>----- Original Message ----
From: Christian Knudsen
Subject: Re: [SDL] sdl_mixer: am I changing music correctly?

I’m thinking it’s an issue with playing MIDI files, since a user that
experienced these crashes haven’t experienced them since changing from MIDI to
OGG music files. I’ll just ship the game with OGG music files instead of MIDI
and hope that resolves it.