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

Author Topic: sf::Sound consumes OpenAL resources  (Read 2726 times)

0 Members and 1 Guest are viewing this topic.

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
sf::Sound consumes OpenAL resources
« on: March 01, 2013, 06:51:15 am »
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?
« Last Edit: March 01, 2013, 06:57:59 am by Disch »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Sound consumes OpenAL resources
« Reply #1 on: March 01, 2013, 07:58:58 am »
I don't think that allocating an OpenAL sound is heavy. Just because a class uses functions from another library, doesn't mean that it magically becomes "heavy".

The only important thing to remember is that there's a maximum number of sound sources that can exist, and this is (should be ;D) clearly documented.

Regarding the crash, things could probably be improved, yes.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
AW: sf::Sound consumes OpenAL resources
« Reply #2 on: March 01, 2013, 08:15:39 am »
I found the maximum notice only in the tutorial, might be also useful in the docs. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/