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

Author Topic: sf::Sound and sf::SoundBuffer declaration order crash  (Read 4859 times)

0 Members and 1 Guest are viewing this topic.

myroidtc

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • Myroid-Type Comics
    • Email
sf::Sound and sf::SoundBuffer declaration order crash
« on: December 18, 2014, 04:31:57 am »
I'm not sure if this is on my end or not. I'm using static-linked SFML 2.2, VC 2012, 64-bit Windows.

This works:
Code: [Select]
int main() {
   sf::SoundBuffer buffer;
   sf::Sound sound;
   buffer.loadFromFile("foo.wav");
   sound.setBuffer(buffer);
   sound.play();
   return 0;
}

But this doesn't:
Code: [Select]
int main() {
   sf::Sound sound;
   sf::SoundBuffer buffer;
   buffer.loadFromFile("foo.wav");
   sound.setBuffer(buffer);
   sound.play();
   return 0;
}

Specifically, declaring the sf::Sound before the sf::SoundBuffer it uses causes a crash and references XTree and some iterator error. The crash happens during the SoundBuffer destructor. This is new to me in 2.2 and wasn't the case in 2.1 stable.

Is this intended behavior?
« Last Edit: January 21, 2015, 05:03:36 pm by myroidtc »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: sf::Sound and sf::SoundBuffer declaration order crash
« Reply #1 on: December 18, 2014, 07:15:09 am »
I'd guess the problem is with the order the objects are destroyed in.
When you declare the soundbuffer last it will be destroyed first when leaving the scope (main), thus for a short while (and when it itself is destroyed) the sound will reference a soundbuffer that no longer exist.
« Last Edit: December 18, 2014, 07:53:36 am by Jesper Juhl »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: sf::Sound and sf::SoundBuffer declaration order crash
« Reply #2 on: December 19, 2014, 04:01:19 pm »
What Jesper said  :P


This has come up before. I've definitely seen it on the forum already somewhere (did you search?) so it's been a 'feature' for a while.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Tommy22pa

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: sf::Sound and sf::SoundBuffer declaration order crash
« Reply #3 on: January 19, 2015, 10:03:42 am »
This works:
int main() {
        sf::Sound sound;
        sf::SoundBuffer buffer;
        buffer.loadFromFile("foo.wav");
        sound.setBuffer(buffer);
        sound.play();
        sound.stop();
        sound.resetBuffer();
        return 0;
}
 

 ;) ;) ;) ;)

« Last Edit: January 19, 2015, 10:13:03 am by Laurent »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: sf::Sound and sf::SoundBuffer declaration order crash
« Reply #4 on: January 19, 2015, 11:21:23 am »
Ehh. Why not just declare the SoundBuffer first so things get destroyed in the proper order?


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Sound and sf::SoundBuffer declaration order crash
« Reply #6 on: January 19, 2015, 01:16:43 pm »
How is this other thread related to the problem?
Laurent Gomila - SFML developer

Tommy22pa

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: sf::Sound and sf::SoundBuffer declaration order crash
« Reply #7 on: January 19, 2015, 04:43:08 pm »
I hope not.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: sf::Sound and sf::SoundBuffer declaration order crash
« Reply #8 on: January 20, 2015, 08:05:10 am »
Then why bring it up?

The problem is obvious; objects created in wrong order.
The simple correct fix is to create them in the proper order.