Thread: crashing when switch music type

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    crashing when switch music type

    Ok, in my game I have so you can select a mid or wav file to play as music. For some reason if I open a mid, another mid and a wav it crashes. I'm using the Windows API and fmod to play my music.

    Is there anything wrong with my method of accomplishing things?
    In my program I have an integer to track whether the current file is a mid or wav and then in my Windows procdure I use the approiate functions to the play the right type music.
    Code:
    #define MUSIC_MID 0
    #define MUSIC_WAV 1
    int music_type = 0;
    
       if(GetOpenFileName(&ofn))
       {   
          FSOUND_Sample_Free(wav_music);         
          FMUSIC_FreeSong(music);         
              
          if(strstr(szFileName,".wav"))
          {
             musicType = MUSIC_WAV;
             wav_music = FSOUND_Sample_Load(2,szFileName,FSOUND_LOOP_NORMAL, 0, 0);
             FSOUND_PlaySound(2,wav_music);               
          }
          else if(strstr(szFileName,".mid"))
          {           
             musicType = MUSIC_MID;
             music = FMUSIC_LoadSong(szFileName);
             FMUSIC_SetLooping(music,TRUE);
             FMUSIC_PlaySong(music);
          }
       }
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Is there anything unusual about the filenames?
    Like sound.mid.wav

    Also, if you have some test code to just play those 3 sounds, does it also crash?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    where exactly is your program crashing in that code? Can you run the debugger and see where it's crashing at? You might be accessing a NULL pointer or something, and not even realizing it.

    the problem could possibly be coming from this code:
    Code:
    FSOUND_Sample_Free(wav_music);
    FMUSIC_FreeSong(music);
    One of those two pointers might not be NULL, and you're trying to basically clear a NULL pointer...

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I know nothing about FMOD and I detest using the Windows API to do anything in a game.

    However it is possible that FMOD has a bug in it that you are not aware of. Switching music types should do nothing with pointers or anything like that. Music is stored in memory and the data type of the music should be controlled and handled by FMOD not by you.

    I would seriously recommend using DirectMusic to do this for you as it's interface is extremely simple. The DirectMusic component of DirectX is uber-powerful but the good thing is you don't have to know a lot to get good sound. Check out a book on it or something.

    I personally would recommend DirectX 9 Audio Exposed -> look it up on amazon.

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    FMOD does use pointers with relation to the value returned when you load a song in to memory.

    from the fmod 3.7.1 documentation:

    FMUSIC_MODULE * F_API FMUSIC_LoadSong(
    const char *
    name
    );

    Returns a pointer to an FMUSIC_MODULE. This is where the trouble could possibly occur, because when you call
    signed char F_API FMUSIC_FreeSong(
    FMUSIC_MODULE *mod
    );

    you could potentially pass a pointer that is just random somewhere (because it hasn't been initialized yet) or a NULL one (which should be caught automatically, but who knows)

    I'd suggest just looking at your free functions that frees the music and make sure that the pointers are valid first.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    No, this is nothing special about the filename. The problem is probabley when freeing the sounds. I have no experience on using a debugger so its of very little value to me.

    Anyways, I have change the section code that frees the sounds to have if statements, but doesn't make it better.
    Code:
    if(wav_music)
       FSOUND_Sample_Free(wav_music);
    if(music)
       FMUSIC_FreeSong(music);
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  7. #7
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    make sure that you are initializing the pointers right away to NULL, or else when initialized, they will be given a random value and then they will be deemed as non-NULL, and you'll try and delete them


    Also, if you are in visual C++ 6.0, just make sure you are compiling in Debug mode, not Release mode, and hit F5, this will start the debugger. Now, run the program just like you normally would and when it crashes, the debugger will tell you exactly where it happened.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Ok. I fixed this problem by initalizing the pointers to NULL after I free their data.
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  2. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  3. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM