diff --git a/src/xpdev/xpbeep.c b/src/xpdev/xpbeep.c index bc2ec9fb6dbba3c39c5bf9584032ffc50521523c..1e4fb59c0dd7cfa2f18fe40dcaee0ee31d0d4bfa 100644 --- a/src/xpdev/xpbeep.c +++ b/src/xpdev/xpbeep.c @@ -826,6 +826,10 @@ error_return: sample_thread_running=FALSE; } +/* + * This MUST not return false after sample goes into the sample buffer in the background. + * If it does, the caller won't be able to free() it. + */ BOOL DLLCALL xp_play_sample(const unsigned char *sample, size_t size, BOOL background) { if(!sample_initialized) { @@ -985,6 +989,7 @@ BOOL DLLCALL xptone(double freq, DWORD duration, enum WAVE_SHAPE shape) { unsigned char *wave; int samples; + BOOL ret; wave=(unsigned char *)malloc(S_RATE*15/2+1); if(!wave) @@ -1004,13 +1009,17 @@ BOOL DLLCALL xptone(double freq, DWORD duration, enum WAVE_SHAPE shape) ; sample_len++; while(samples > S_RATE*15/2) { - if(!xp_play_sample(wave, sample_len, TRUE)) + if(!xp_play_sample(wave, sample_len, TRUE)) { + free(wave); return FALSE; + } samples -= sample_len; } } makewave(freq,wave,samples,shape); - return(xp_play_sample(wave, samples, FALSE)); + ret = xp_play_sample(wave, samples, FALSE); + free(wave); + return ret; } #ifdef __unix__