Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Exception using sf::music  (Read 2912 times)

0 Members and 1 Guest are viewing this topic.

fabvman

  • Newbie
  • *
  • Posts: 11
    • View Profile
Exception using sf::music
« on: March 10, 2011, 05:32:15 pm »
Hi. Well my problem is this: I have a small game, which is divided into three "stages. " The first is the initial screen (it only shows a static image) and has no music, then pressing a key change to the next stage. The second stage is the game screen. Here I start to play background music (by pressing a key in the initial stage, I call Play of the object "sf:: Music "). Then, win or lose the game, I move to a third stage (the final stage). In the final stage, I draw the last frame I drew in the game screen, and also a message (sf:: string) that says if you won or not. Here what I do is stop playing the music (when I'm checking if he wins or loses in the game screen I pause playback). Also what I do is give to the player the possibility to restart the game(returning to the game screen). When I do this I want to play music again and visual studio 2008 throws an exception.
What could be the problem? I'm using pointers to a single object "sf:: music".
I could not find a similar problem in the forum.
I tried to play and pause the music within the game screen by using two different keys and it works perfect. The problem is when I want to change the final stage to the game screen again.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Exception using sf::music
« Reply #1 on: March 10, 2011, 05:44:26 pm »
We won't be able to help you with only a description of how your game works ;)

Here is what you can do:
- use the debugger! it's so easy to see what's wrong with it
- ...or show us a minimal and complete piece of code that reproduces your problem
- ...or show us the relevant parts of your original code
Laurent Gomila - SFML developer

fabvman

  • Newbie
  • *
  • Posts: 11
    • View Profile
Exception using sf::music
« Reply #2 on: March 10, 2011, 06:30:12 pm »
You're right :D. Well, I'll write some pieces of code.
The function that handles the keyboard input to change the stage is:

 
Code: [Select]
while(app->GetEvent(*Event))
{
if(Event->Type == sf::Event::KeyPressed)
{
if(Event->Key.Code == sf::Key::Escape)
app->Close();

switch(stage)
{
case INITIALSTAGE:
{
if(Event->Key.Code == sf::Key::Space)
{
Initialize();
backgroundMusic->Play();
}

break;
}

case GAMESTAGE:
{
if(Event->Key.Code == sf::Key::Space)
Initialize();

break;
}

case FINALSTAGE:
{
if(Event->Key.Code == sf::Key::N)
app->Close();

if(Event->Key.Code == sf::Key::S)
{
Initialize();
                                                / / here I had written: backgroundMusic-> Play () / / at this point throws the exception
}

break;
}
}
}
}

Then at the end of my function "Update" I check the status of the game (If I win or lose).

Code: [Select]

if(lifes == 0)
{
state = LOSE;
stage = FINALSTAGE;
backgroundMusic->Stop();
}

else if(points == 1000)
{
state = WIN;
stage = FINALSTAGE;
backgroundMusic->Stop();
}


Game loop is:
Code: [Select]

Initialize();

clock->Reset();

while(app->IsOpened())
{
ManageKeyboardInput();

CollisionDetection();

Update();

Draw();
}


This is only part of the finished code. If you need it I can send you the finished code

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Exception using sf::music
« Reply #3 on: March 10, 2011, 06:36:19 pm »
It's hard to tell with pieces of a bigger project... A minimal code would be great (but yes, it requires that you spend some time to write it).

But we can start with the debugger. Even if you're not a debugger master you can just run your app in debug mode (F5) and:
1. read the error message in the crash popup window
2. see where (file and line) the debugger stopped
3. examinate the callstack (Debug menu -> Windows -> Call stack) to see the sequence of calls that lead to the crash

That should be enough to start.
Laurent Gomila - SFML developer

fabvman

  • Newbie
  • *
  • Posts: 11
    • View Profile
Exception using sf::music
« Reply #4 on: March 10, 2011, 07:05:57 pm »
Hi, I did that.
The error  message of the exception is:

"Unhandled exception at 0x00f4cd86 in TP4Final.exe: 0xC0000005: Access violation reading location 0x736c7584."

Last instructions on the stack are:

"tp4final.exe!sf::SoundStream::Play() + 0c36 bytes"
"tp4final.exe!Game::ManageKeyboardInput() Line 218"

The last line (sf::soundstream......) has a yellow arrow at the left side and the game trows the exception there. When I do double click in that line, the next warning shows up:

"There is no source code available for the current location".

I don't know what it could be.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Exception using sf::music
« Reply #5 on: March 10, 2011, 07:12:52 pm »
Are you sure that your music pointer is still valid at that moment? Maybe you deleted it earlier?
Laurent Gomila - SFML developer

fabvman

  • Newbie
  • *
  • Posts: 11
    • View Profile
Exception using sf::music
« Reply #6 on: March 10, 2011, 07:44:25 pm »
Yes, I only delete the pointer when the destructor of the game class is called.
I tried this at the manageKeyboardInput class:
Code: [Select]
case FINALSTAGE:
{
if(Event->Key.Code == sf::Key::N)
app->Close();

if(Event->Key.Code == sf::Key::S)
{
Initialize();
backgroundMusic = new sf::Music();
       backgroundMusic->OpenFromFile("Ambient.ogg");
backgroundMusic->SetLoop(true);
       backgroundMusic->Play();
}

break;
}


This worked perfect. I looked around the code and I could not find any statement where I lose the pointer (using delete or otherwise).
What it could be? I still don't know.

 

anything