Skip to content

Commit

Permalink
Fix filehandle leaks in Sound module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinnegatamante committed Oct 23, 2024
1 parent 3dde7fd commit b7dcc59
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions source/luaSound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,13 @@ static int audioThread(unsigned int args, void* arg){

// Initializing audio decoder
audio_decoder[id] = AudioDecoder::Create(mus->handle, "Track");
if (audio_decoder[id] == NULL){ // TODO: Find why this case apparently can happen
if (mus->tempBlock){
if (audio_decoder[id] == NULL) { // TODO: Find why this case apparently can happen
if (mus->tempBlock) {
fclose(mus->handle);
free(mus);
mus = NULL;
}else{
mus->handle = fopen(mus->filepath,"rb");
fseek(mus->handle, 0, SEEK_SET);
mus->audioThread = 0xFF;
}
availThreads[id] = true;
Expand All @@ -120,13 +121,14 @@ static int audioThread(unsigned int args, void* arg){
AudioDecoder::Format fmt;
audio_decoder[id]->GetFormat(rate, fmt, chns);

if (rate != 48000){ // That should not happen
if (rate != 48000) { // That should not happen
audio_decoder[id].reset();
if (mus->tempBlock){
if (mus->tempBlock) {
fclose(mus->handle);
free(mus);
mus = NULL;
}else{
mus->handle = fopen(mus->filepath,"rb");
} else {
fseek(mus->handle, 0, SEEK_SET);
mus->audioThread = 0xFF;
}
availThreads[id] = true;
Expand All @@ -141,14 +143,15 @@ static int audioThread(unsigned int args, void* arg){
mus->cur_audiobuf = mus->audiobuf;

// Audio playback loop
for (;;){
for (;;) {

// Check if the music must be paused
if (mus->pauseTrigger || mustExit){
if (mus->pauseTrigger || mustExit) {

// Check if the music must be closed
if (mus->closeTrigger){
if (mus->closeTrigger) {
audio_decoder[id].reset();
fclose(mus->handle);
free(mus->audiobuf);
free(mus->audiobuf2);
free(mus);
Expand All @@ -158,10 +161,10 @@ static int audioThread(unsigned int args, void* arg){
}

// Check if the thread must be closed
if (mustExit){
if (mustExit) {

// Check if the audio stream has already been closed
if (mus != NULL){
if (mus != NULL) {
mus->closeTrigger = true;
continue;
}
Expand All @@ -179,37 +182,39 @@ static int audioThread(unsigned int args, void* arg){
}

// Check if a volume change request arrived
if (mus->volume != old_volume){
if (mus->volume != old_volume) {
int vol_stereo_new[] = {mus->volume, mus->volume};
sceAudioOutSetVolume(ch, (SceAudioOutChannelFlag)(SCE_AUDIO_VOLUME_FLAG_L_CH | SCE_AUDIO_VOLUME_FLAG_R_CH), vol_stereo_new);
old_volume = mus->volume;
}

if (mus->isPlaying){

if (mus->isPlaying) {
// Check if audio playback finished
if ((!mus->loop) && audio_decoder[id]->IsFinished()) mus->isPlaying = false;
if ((!mus->loop) && audio_decoder[id]->IsFinished())
mus->isPlaying = false;

// Update audio output
if (mus->cur_audiobuf == mus->audiobuf) mus->cur_audiobuf = mus->audiobuf2;
else mus->cur_audiobuf = mus->audiobuf;
if (mus->cur_audiobuf == mus->audiobuf)
mus->cur_audiobuf = mus->audiobuf2;
else
mus->cur_audiobuf = mus->audiobuf;
audio_decoder[id]->Decode(mus->cur_audiobuf, (chns > 1) ? BUFSIZE : BUFSIZE_MONO);
sceAudioOutOutput(ch, mus->cur_audiobuf);

}else{

} else {
// Check if we finished a non-looping audio playback
if ((!mus->loop) && audio_decoder[id]->IsFinished()){
if ((!mus->loop) && audio_decoder[id]->IsFinished()) {

// Releasing the audio file
audio_decoder[id].reset();
if (mus->tempBlock){
if (mus->tempBlock) {
fclose(mus->handle);
free(mus->audiobuf);
free(mus->audiobuf2);
free(mus);
mus = NULL;
}else{
mus->handle = fopen(mus->filepath,"rb");
} else {
fseek(mus->handle, 0, SEEK_SET);
mus->audioThread = 0xFF;
}
availThreads[id] = true;
Expand Down

0 comments on commit b7dcc59

Please sign in to comment.