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

Author Topic: [SOLVED] Problem with Singletons  (Read 2552 times)

0 Members and 1 Guest are viewing this topic.

daywithstars

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
[SOLVED] Problem with Singletons
« on: May 09, 2023, 12:06:30 am »
There is some problem if I call the destructor of a sf::SoundBuffer before it reaches the normal end of scope!? because I am using a singleton class to store global sounds that can be used for any class in the application, but, the aplication is big enough to require a more smart loading instead load everything at the beginning of the application.

Every time I free those sound buffers by just removing its unique_ptr from a map, when the application reaches the end I've got: 'malloc(): unsorted double linked list corrupted', maybe there is some restriction because the sf::SoundFileFactory that keeps some global info, that will properly RAII at the end of application only!?

For instance, I've only loaded the buffers and freed them. I did not create any sf::Sound or play it, just loaded and freed it by removing the unique_ptr from the map using its 'key' on the 'erase' method.
« Last Edit: May 19, 2023, 01:19:17 pm by daywithstars »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem with Singletons
« Reply #1 on: May 09, 2023, 02:53:21 am »
Using SFML classes globally is not recommended. The big ones (resources, windows etc.) tend to require their own control of the internals and globals, for one, can cause things to be created out of order or before or after it's ready for it.

It's worth noting that SoundBuffers are OpenAL resources so it may even be out of SFML's hands!

It's best to keep resources (especially SFML ones) within local scope, even if that local scope is main and it's passed around everywhere! ;D
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11008
    • View Profile
    • development blog
    • Email
Re: Problem with Singletons
« Reply #2 on: May 09, 2023, 01:30:11 pm »
What Hapax said and you should also never manually call a destructor!  :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

daywithstars

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Problem with Singletons
« Reply #3 on: May 19, 2023, 01:18:38 pm »
Alright then, so, when comes to the problem of scope I will not be using those SFML resources in Sigleton classes. Thanks  ;).