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
Remove sound default constructor #2640
Remove sound default constructor #2640
Conversation
so that a Sound instance is always bound to a SoundBuffer
because the pointer now cannot be null
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## master #2640 +/- ##
==========================================
+ Coverage 30.26% 30.69% +0.43%
==========================================
Files 229 229
Lines 19748 19740 -8
Branches 4723 4718 -5
==========================================
+ Hits 5976 6060 +84
- Misses 13046 13055 +9
+ Partials 726 625 -101
... and 55 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
|
I added a test program. I found no issue playing around with it. |
cmake_minimum_required(VERSION 3.22)
project(audio-test CXX)
include(FetchContent)
FetchContent_Declare(SFML
GIT_REPOSITORY https://github.com/kimci86/SFML.git
GIT_TAG feature/remove_sound_default_constructor)
FetchContent_MakeAvailable(SFML)
add_executable(audio-test main.cpp)
target_link_libraries(audio-test PRIVATE SFML::Graphics SFML::Audio) Here's how you can build this branch to run the example program. I ran this and couldn't notice any oddities so this change appears to hold up to testing. |
I just inserted this code to the testing example from above: case sf::Keyboard::Scan::Num3:
{
auto bufferTemp = sf::SoundBuffer{};
if (!bufferTemp.loadFromFile("resources/jingles_SAX03.ogg"))
return EXIT_FAILURE;
sound2.setBuffer(bufferTemp);
sound2.play();
break;
} In debug mode this fails in the added |
This is getting at how The only way to turn this into a hard failure in a destructor in release mode is to throw an exception or call some variety of program-ending function like |
I guess in that case, OpenAL detects that the OpenAL buffer is still in use and refuses to delete it, so we are leaking the resource then. |
89f6373
to
7013145
Compare
Here we go. Now debug and release are the same. |
7013145
to
6ef894e
Compare
{ | ||
setBuffer(buffer); | ||
m_buffer->attachSound(this); | ||
alCheck(alSourcei(m_source, AL_BUFFER, static_cast<ALint>(m_buffer->m_buffer))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really a deal breaker, but we've duplicated these two lines in three places now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree this duplication it is not extremely elegant. The motivation here is that only constructors are responsible for setting up the invariant (i.e. sound it attached to a sound buffer), and methods (especially setBuffer) can be simpler by assuming the invariant is true.
Description
This PR is related to the issue #2507.
By removing
Sound
default constructor, we make sure it is always bound to someSoundBuffer
.Doing some operations on an OpenAL sound buffer while OpenAL Sources are attached to it triggers error messages, so we have to keep track of
Sound
instances which are bound to a givenSoundBuffer
so that we can detach and attach sounds when needed.Tasks
How to test this PR?
voip
,sound
,sound_capture
andtennis
.resources.zip
Sounds are from kenney.nl