I just discovered some craziness today and thought it would be nice if the documentation gave some kind of warning.
I had assumed that sf::Sound was a lightweight class similar to sf::Sprite -- where you can "carelessly" create instances and all you have to worry about is a little extra memory usage. With this in mind... in a game I'm working on, we would assign sf::Sounds to objects similar to how we'd assign sf::Sprites.
That is... if an object draws a quad, it owns a sf::Sprite. If it plays a sound, it owns a sf::Sound. Makes sense, right?
I was in for a rude awakening when all of the sudden I discovered stderr flooded with OpenAL failures and various crashes when I'd try to play sound effects (and on shutdown).
As I looked closer at the error message and did some debugging and examination of sf::Sound... I came to a startling realization:
sf::Sound allocates OpenAL resources and reserves them throughout its lifetime. OpenAL resources are limited... so this makes sf::Sound a heavy resource class that should be used with care. Instances should not be frivolously created/destroyed.
In my case, what was happening was that each object owned 4 Sounds, and there were 100 objects. At some point (I didn't figure out exactly when), I had consumed all the available OpenAL resources, and additional calls to alGenSources (in the sf::SoundSource default ctor) were failing. This led to a chain of errors (and ultimately crashes) due to the m_source member being uninitialized and passed to various AL functions.
Just to clarify.... I do not think this is a design issue with SFML. It makes perfect sense for sf::Sound to behave this way. I just wanted to throw this out there as a consideration.
I would like to see:
1) Some kind of warning in the docs for sf::Sound (or really, sf::SoundSource and all derived classes) that these classes are resource heavy and should be used with care.
2) SFML should not crash under any circumstances, even if initialization fails. In this case... SoundSource::m_source should probably be initialized with 0 or some other value if the call to alGenSources fails. I would even be okay with an exception being thrown.
Thanks.
-Disch
EDIT: I should clarify I'm using SFML 2.0 ... I think rc 146 but am not 100% sure. Is there a way to easily check?