flutter_soloud
flutter_soloud copied to clipboard
docs: What does disposeSound actually do?
Description
I'm trying to understand (and hopefully document) the function of disposeSound
. It seems that C++ SoLoud has no direct dispose()
equivalent. The closest I could find is the destructor (AudioSource::~AudioSource()
) in soloud_audiosource.cpp:208
, which just calls stop()
, which in turn calls Soloud::stopAudioSource()
, which calls stopVoice_internal()
. I'm not sure if we're calling this from Dart via FFI at all.
My question is: when my game plays 10 songs in a row, do all these songs just stay in (C++) memory forever? I'm assuming their data is not in memory (because LoadMode.disk
) but still.
From what I can understand, our disposeSound()
only removes the AudioSource from activeSounds
(on the Dart side) and from sounds
(on the Player
/ player.cpp
) side. No SoLoud
API is called. (Or is the destructor called because the ActiveSound is erased from the sounds
vector?)
I hope I can express everything correctly, feel free to question me.
It seems that C++ SoLoud has no direct
dispose()
equivalent. The closest I could find is the destructor (AudioSource::~AudioSource()
) insoloud_audiosource.cpp:208
, which just callsstop()
, which in turn callsSoloud::stopAudioSource()
, which callsstopVoice_internal()
. I'm not sure if we're calling this from Dart via FFI at all.
Exact. Those methods are called with SoLoud.disposeSound()
or SoLoud.disposeAllSound()
which internally calls AudioSource::~AudioSource()
.
The SoLoud.stop()
just stops the handle, but doesn't dispose the SoLoud.AudioSource
.
A remark: the AudioSource
in Soloud lib, can be any kind of audio: a LoadMode.memory
, a LoadMode.disk
or even a text to speech sound. It can be subclassed to have more modes like maybe a streaming from an internet radio or a remote audio file.
My question is: when my game plays 10 songs in a row, do all these songs just stay in (C++) memory forever? I'm assuming their data is not in memory (because
LoadMode.disk
) but still.
I did some tests:
- load into mem 14 songs
- I see increasing system memory of about 2 GB
- I used
SoLoud.instance.disposeSound
for each songs (also tried withSoLoud.instance.disposeAllSound()
) - I see decreasing system memory of about 2 GB
using the functions mentioned above, the sound(s) is cleared disposing also the SoLoud::AudioSource
which also free the memory.
From what I can understand, our
disposeSound()
only removes the AudioSource fromactiveSounds
(on the Dart side) and fromsounds
(on thePlayer
/player.cpp
) side. NoSoLoud
API is called. (Or is the destructor called because the ActiveSound is erased from thesounds
vector?)
For the above considerations, yes, the destructor is called when removing the sound from vector.
If you see the memory not freed, can you please paste some code to try? I tried the following:
await SoLoud.instance.initialize();
final audioSources = <AudioSource>[];
audioSources.add(await SoLoud.instance.loadFile('complete/audio/file/path.mp3'));
// add here as many audio file you want (whoops, no! Less the 16 :) )
for (final s in audioSources) {
await SoLoud.instance.play(s);
}
await delay(5000); // breakpoint here to see memory increasing
// for (final s in audioSources) {
// await SoLoud.instance.disposeSound(s);
// }
await SoLoud.instance.disposeAllSound();
SoLoud.instance.deinit(); // breakpoint here to see memory decreasing
Thank you so much! This is exactly what I wanted to know. I really appreciate you confirmed it experimentally, too.
I'll keep this open until I incorporate this information into Dart doc comments (but it's already useful for the codelab I'm working on).