New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't have global sound objects #970
Comments
|
I had hoped you'd read my comment, but I guess you didn't... The destruction order of global objects is undefined. So if OpenAL's globals get destroyed before SFML's global get destroyed you end up with a crash. As I said and you said yourself, don't use global SFML resources and you'll be fine. Edit: Don't forget to follow the Contribution Guidelines. |
|
And as I said, it's not easy to change my code to avoid using global SFML resources. (I'm not certain it's actually possible.) However, it's entirely possible to guarantee that an object is not destroyed until after other objects depending on it (it's not even all that hard to be honest), so it's not like this isn't fixable on your end. I also strongly disagree that it's not an issue - a crash is a crash is an issue in my opinion. I can see that you don't care or want to fix it, but I would be willing to try. Would you at least consider accepting it if I made a pull request for this? EDIT: By the way, it looks like the code is already set up to try to guarantee correct order of destruction, but it's not working in this case. |
It's almost always possible to find a design that doesn't use global variables. Of course you have existing code, so refactoring will be required. Using a more structured design is a good idea anyway, it will save you headaches in many other places, too.
Yes, and we constantly recommend not to use SFML objects, especially not resource-related ones, in global scope. Don't blame the library for deliberately ignoring such advice.
No, it's not directly possible, not as long as you maintain ownership and deterministic destruction semantics. In C++, it has always been the default paradigm that objects live during their scope, and dependencies must outlive their clients. Deviating from that common practice needs considerable book-keeping effort and adds a lot of complexity and overhead to manage dependencies correctly (no, it's not as simple as adding To get a bit of an idea how "simple" it is to create/destroy things at global scope in defined order, have a look at the longevity pattern and singletons discussed by Alexandrescu. They're explained to their full extent in his book Modern C++ Design, the Loki library is also a good source of inspiration. |
|
I looked into it a bit and it seems the problem is related to OpenAL's use of I do think this issue should be left open in case someone thinks of a reasonable way to fix it, but I suppose I can at least prevent the crash now that I know the reason for it. |
I'm afraid that there's probably none. If you look at the longevity pattern, you'll see that it also fiddles with Those guidelines to avoid global variables in general and for SFML in particular exist for a reason... And in C++, you always have to bear the consequences if you don't follow good practices 😉 |
|
For the record, the SFML code in Sound.cpp currently appears to be attempting something vaguely similar to your PhoenixSingleton, but it doesn't work because OpenAL's Using pointers like I mentioned above seems to have worked for me, in any case. |
Using a global
sf::Soundobject causes the program to crash on exit with an assertion failure. I realize that using global objects is frowned upon, but it would be nontrivial to rewrite my program to not use them. This happens with straight globals, class statics, function statics, and file statics, meaning there's no good workaround. I believe it started when I upgraded SFML to 2.2, or maybe 2.1 - it hasn't always been an issue.The following minimal code:
when compiled with
clang++ test.cpp -framework SFML -framework sfml-audio, produces the following output and terminates with SIGABRT:The text was updated successfully, but these errors were encountered: