Skip to content

Commit

Permalink
Mix_LoadWAV_RW, Mix_FreeChunk: use correct function to free chunk->abuf.
Browse files Browse the repository at this point in the history
chunk->abuf allocated by SDL_LoadWAV_RW should be freed by SDL_FreeWAV.
Ported from SDL-1.2 branch commit 4c47f6f for sake of being by the book.

Reference issue: libsdl-org/sdl12-compat#215
  • Loading branch information
sezero committed Oct 7, 2022
1 parent d792a5f commit db9da3d
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/mixer.c
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ Mix_Chunk *Mix_LoadWAV_RW(SDL_RWops *src, int freesrc)
SDL_AudioSpec wavespec, *loaded;
SDL_AudioCVT wavecvt;
int samplesize;
int wavfree; /* to decide how to free chunk->abuf. */

/* rcg06012001 Make sure src is valid */
if (!src) {
Expand Down Expand Up @@ -786,7 +787,9 @@ Mix_Chunk *Mix_LoadWAV_RW(SDL_RWops *src, int freesrc)
/* Seek backwards for compatibility with older loaders */
SDL_RWseek(src, -4, RW_SEEK_CUR);

wavfree = 0;
if (SDL_memcmp(magic, "WAVE", 4) == 0 || SDL_memcmp(magic, "RIFF", 4) == 0) {
wavfree = 1;
loaded = SDL_LoadWAV_RW(src, freesrc, &wavespec, (Uint8 **)&chunk->abuf, &chunk->alen);
} else if (SDL_memcmp(magic, "FORM", 4) == 0) {
loaded = Mix_LoadAIFF_RW(src, freesrc, &wavespec, (Uint8 **)&chunk->abuf, &chunk->alen);
Expand All @@ -813,21 +816,33 @@ Mix_Chunk *Mix_LoadWAV_RW(SDL_RWops *src, int freesrc)
if (SDL_BuildAudioCVT(&wavecvt,
wavespec.format, wavespec.channels, wavespec.freq,
mixer.format, mixer.channels, mixer.freq) < 0) {
SDL_free(chunk->abuf);
if (wavfree) {
SDL_FreeWAV(chunk->abuf);
} else {
SDL_free(chunk->abuf);
}
SDL_free(chunk);
return(NULL);
}
samplesize = ((wavespec.format & 0xFF)/8)*wavespec.channels;
wavecvt.len = chunk->alen & ~(samplesize-1);
wavecvt.len = chunk->alen & ~(samplesize - 1);
wavecvt.buf = (Uint8 *)SDL_calloc(1, wavecvt.len*wavecvt.len_mult);
if (wavecvt.buf == NULL) {
Mix_OutOfMemory();
SDL_free(chunk->abuf);
if (wavfree) {
SDL_FreeWAV(chunk->abuf);
} else {
SDL_free(chunk->abuf);
}
SDL_free(chunk);
return(NULL);
}
SDL_memcpy(wavecvt.buf, chunk->abuf, wavecvt.len);
SDL_free(chunk->abuf);
if (wavfree) {
SDL_FreeWAV(chunk->abuf);
} else {
SDL_free(chunk->abuf);
}

/* Run the audio converter */
if (SDL_ConvertAudio(&wavecvt) < 0) {
Expand All @@ -838,9 +853,10 @@ Mix_Chunk *Mix_LoadWAV_RW(SDL_RWops *src, int freesrc)

chunk->abuf = wavecvt.buf;
chunk->alen = wavecvt.len_cvt;
wavfree = 0;
}

chunk->allocated = 1;
chunk->allocated = (wavfree == 0) ? 1 : 2; /* see Mix_FreeChunk() */
chunk->volume = MIX_MAX_VOLUME;

return(chunk);
Expand Down Expand Up @@ -946,8 +962,13 @@ void Mix_FreeChunk(Mix_Chunk *chunk)
}
Mix_UnlockAudio();
/* Actually free the chunk */
if (chunk->allocated) {
switch (chunk->allocated) {
case 1:
SDL_free(chunk->abuf);
break;
case 2:
SDL_FreeWAV(chunk->abuf);
break;
}
SDL_free(chunk);
}
Expand Down

0 comments on commit db9da3d

Please sign in to comment.