0
0

Hello.

I just have a little problem. My program do segfault when i release a sound that was used.

I really do not understand why, maybe you can help.

The thing I do to release is to first stop playing the sound if it is playing, then use the Sample_Free function.

Im’ sure that the sound is correctly load, and I check before the Sample_Free if the sample I’m going to release have not hits pointer set to NULL. So This is bad and I did not see why it does a segfault.

I’ve got this problem on Linux.

  • You must to post comments
0
0

I just try to cornered a bit the thing. But without findingg the real problem :/

Here the backtrace obtained with gdb after the segfault :
[quote:2uc6tp6k]Program received signal SIGSEGV, Segmentation fault.
0x4011465c in FSOUND_Sample_Free () from /usr/lib/libfmod-3.71.so
(gdb) backtrace

0 0x4011465c in FSOUND_Sample_Free () from /usr/lib/libfmod-3.71.so

1 0x4039b9a0 in sys_sigabbrev () from /lib/i686/libc.so.6

2 0x08054cdb in deleteListSound(sndsLst*&) (lst=@0x811a368) at ListSound.h:87

3 0x0805601b in ~ImageAnimated (this=0x811a338) at ImageAnimated.h:321

4 0x08056fdd in ~Character (this=0x81092c8) at Character.h:59

5 0x080583c5 in deleteCharacter(charsLst*&) (l=@0x80c9920)

at ListCharacter.h:36

6 0x0805e337 in ~Game (this=0x80c9908) at Game.h:57

7 0x0805f461 in main (argc=1, argv=0xbffff774) at src/main.cpp:52

8 0x40284c57 in __libc_start_main () from /lib/i686/libc.so.6[/quote:2uc6tp6k]

And here is my class managing sounds :
[code:2uc6tp6k]class Sound
{
private:
//The three kind of sound that exist
FSOUND_SAMPLE *sample;
FSOUND_STREAM *stream;
FMUSIC_MODULE *module;
bool Playing; //Is the sound currently beeing played
int fileType; //1 => SAMPLE, 2 => STREAM, 3 => MUSIC
int channel; //For some sounds register the sound channel used to stop it

private:
int getExt(string fileName); //Return the extension of a file
char toUpperCase(char c); //Convert a char to it’s upper case equivalent
string toUpperCase(string s); //Convert a string to upper case
int findStr(string src, string s); //search for a string

public:
Sound(string file, bool loop); //Create a new sound
~Sound(); //Delete the sound
void playSound(); //play the sound
bool isPlaying(); //Check if the sound is playing
void stopSound(); //stop playing the sound
};

char Sound::toUpperCase(char c)
{

if(c>=’a’ && c<=’z’) //If we have a lower case letter convert it to the upper case equivalent
{
c=c-32;
}
return c; //Return the modified character
}

int Sound::findStr(string src,string s)
{
int pos=-1;
pos=src.find(s,0); //Search the string s in the src string
if (pos==string::npos)
pos=-1;
return pos; //Return the position of the string find -1 if not found
}

string Sound::toUpperCase(string s)
{
for(int i=0;i<s.length();i++) //For each letter of the string
{
s[i]=this -> toUpperCase(s[i]); //convert each of the string char too upper case
}
return s; //Return the modified string
}

Sound::Sound(string file, bool loop)
{
this -> sample = NULL; //Initialize the thre sound pointers
this -> stream = NULL;
this -> module = NULL;
this -> Playing = false; //The sound is not playing

this -> fileType = this -> getExt(file); //Get the file extension

switch (this -> fileType)
/* for each case load the sound in the appropriates format and register to play it looped or not /
/
And show error message if cannot load the sound */
{
case 1:
if(loop)
this -> sample = FSOUND_Sample_Load(FSOUND_FREE,file.c_str(),FSOUND_LOOP_NORMAL,0,0);
else
this -> sample = FSOUND_Sample_Load(FSOUND_FREE,file.c_str(),FSOUND_NORMAL,0,0);
if(this -> sample == NULL)
fprintf(stderr,"[ERROR]UNABLE TO LOAD SAMPLE :%s\n", file.c_str());
break;
case 2:
if(loop)
this -> stream = FSOUND_Stream_Open(file.c_str(),FSOUND_LOOP_NORMAL,0,0);
else
this -> stream = FSOUND_Stream_Open(file.c_str(),FSOUND_NORMAL,0,0);
if(this -> stream == NULL)
fprintf(stderr,"[ERROR]UNABLE TO LOAD STREAM :%s [%s]\n", file.c_str(),FMOD_ErrorString(FSOUND_GetError()));
break;
case 3:
this -> module = FMUSIC_LoadSong(file.c_str());
if(module == NULL)
fprintf(stderr,"[ERROR]UNABLE TO LOAD MODULE :%s\n", file.c_str());
break;
default:
fprintf(stderr,"[ERROR]UNABLE TO UNDERSTAND FILE TYPE :%s\n", file.c_str());
break;
}
}

Sound::~Sound()
{
this -> stopSound(); //Stop the sound if it is playing
switch (this -> fileType) //Delete the sound from memory
{
case 1:
if(this -> sample != NULL)
FSOUND_Sample_Free(this -> sample);
break;
case 2:
if(this -> stream != NULL)
FSOUND_Stream_Close(this -> stream);
break;
case 3:
if(this -> module != NULL)
FMUSIC_FreeSong(this -> module);
break;
}
}

int Sound::getExt(string fileName)
{
int placesep;
string tmp;
placesep = this -> findStr(fileName,".");
tmp = fileName.substr(placesep+1,3); //get the file extension
tmp = this -> toUpperCase(tmp); //change the extension to uppercase
//Return the fmod type following the file type
if (tmp=="MP3")
return 2;
if (tmp=="WAV")
return 1;
if (tmp=="MID")
return 2;
if (tmp=="MP2")
return 2;
if (tmp=="RAW")
return 2;
if (tmp=="MOD")
return 3;
if (tmp=="XM")
return 3;
if (tmp=="IT")
return 3;
if (tmp=="S3M")
return 3;
if (tmp=="OGG")
return 2;
return -1;
}

void Sound::playSound()
{
switch (this -> fileType) //Start playing the song depending on the file type
{
case 1:
this -> channel = FSOUND_PlaySound(FSOUND_FREE, this -> sample);
this -> Playing = true; //the song is now played
break;
case 2:
this -> channel = FSOUND_Stream_Play(FSOUND_FREE, this -> stream);
this -> Playing = true; //the song is now played
break;
case 3:
FMUSIC_PlaySong(this -> module);
this -> Playing = true; //the song is now played
break;
}
}

bool Sound::isPlaying()
{
return this -> Playing; //Return if the song is playing or not
}

void Sound::stopSound()
{
if(this -> Playing)
{
switch (this -> fileType) //Stop playing the sound

{
  case 1:
    FSOUND_StopSound(this -&gt; channel);
    break;
  case 2:
    FSOUND_Stream_Stop(this -&gt; stream);
    break;    
  case 3:
    FMUSIC_StopSong(this -&gt; module);    
    break;    
}
this -&gt; Playing = false; //the sound is no more playing

}
}[/code:2uc6tp6k]

Maybe you can find something wrong.

I think I do all hte things, stop playing the sound if it is playing and checking if the pointer to the sound is not null before trying to free the ressources.

  • You must to post comments
0
0

Hello again :p

I use the this, just to clearly make that I use the class members. It is better since my teacher said during my computing studies, but it give the same result if I do it without the [b:5470a396]this[/b:5470a396] keyword

So you would like to know what I do.

The things are always in this order :

[code:5470a396]Sound *sound = new Sound("data/music/intro.mp3", true);

sound -> playSound();
sound -> stopSound();

delete sound;[/code:5470a396]

The playsound and stop sound may be called more than one time each. Because of that I have put a stop sound in the ~Sound() function.

I really do nothing much with them.

  • You must to post comments
0
0

Okay I’ve searched and find the problem, it occured only on wave file.

So i changed the value to to make it load in the Sample instead of stream and there is no more problem. So I must sayed that the error were mine but I could find it with your help

I was loading the wav as stream, and when I switch to sample there is no more problem.

Thanks for the help 😀

  • You must to post comments
0
0

Ok I’ll test this when I’ll be back home.
To see if this is a particular case or not.

  • You must to post comments
Showing 4 results
Your Answer

Please first to submit.