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

Author Topic: sf::Sound nullptr problem with std::map  (Read 995 times)

0 Members and 1 Guest are viewing this topic.


  • Newbie
  • *
  • Posts: 4
    • View Profile
sf::Sound nullptr problem with std::map
« on: September 02, 2019, 06:02:12 pm »
Hi Forum,
so this is a rather complicated problem with sf::Sound.
I face a problem when i create a std::map with an std::string as the key-value and a sf::Sound as the second argument ! I know its not recommended to save an sf::Sound to container as it always needs to point to an sf::SoundBuffer ! But in my case i wanted to go with that solution so i created an std::vector that would store my sf::SoundBuffer ! Before i go on heres the Code:
Header(minimal example):
std::map <std::string, sf::Sound> Sounds;
std::vector<sf::SoundBuffer> soundBuffer;
So here are both map and vector !
Now the implementation from a function that stores a loaded sf::SoundBuffer in a Vector and an sf::Sound to
a Map
sf::SoundBuffer buffer;
if (buffer.loadFromFile(path))
                        //Sounds.insert(std::pair<std::string, sf::Sound>(name, sf::Sound{soundBuffer.back()}));
                        Sounds.emplace(name, sf::Sound{ soundBuffer.back() });
                        return true;
As you can see, i tried out a lot of things

So wheres now the problem ? When i try to play multiple sounds only the last one added is functioning.The other two(in my case i added 3 sounds as an wav) get nullptr(first screenshot).
And the sf::SoundBuffer Vector is perfectly working !
I have no idea what i did wrong(i think it isnt an out of scope problem, because it should be prevented through the Vector).
I hope u can help me !

« Last Edit: September 02, 2019, 06:08:15 pm by Phyton »


  • SFML Team
  • Hero Member
  • *****
  • Posts: 9558
    • View Profile
    • development blog
    • Email
Re: sf::Sound nullptr problem with std::map
« Reply #1 on: September 02, 2019, 08:42:33 pm »
You got the problem backwards. The issue with references isn't that the sound object are in a map, but the issue is that your buffer is in a vector.
The vector guarantees contiguous memory, so when you push_back more objects, the memory needs to be reallocated at one point. This reallocation happens by moving all objects to a new memory location and it's that, that breaks the reference the sound object may hold to the sound buffer.

As such, it's recommended for sound buffers, textures and fonts to not store them in vectors, but use a "stable" container such as a map.

As for your sound objects, I personally recommend a std::deque, as you can push_back new sounds and pop_front sounds that finished playing.
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/


  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: sf::Sound nullptr problem with std::map
« Reply #2 on: September 02, 2019, 09:08:00 pm »
Big thank to you !
I should have known(because obviously a sf::SoundBuffer is quite large) that a push_back moved the content to other adress spaces !
I decided to store my buffers in an std::list and it works like a charme !