fix: plug memory leaks on file reload and texture regen

Three leaks, all at the relevant choke points:

- ComputeSTFTInit reallocated result->segments without freeing the old
  array, and runs twice per load (load site + loadingPhase 0), so every
  file load leaked the STFT. Free the previous result at the top.
- LoadWavFile malloc'd signal->samples without freeing the previous
  buffer; free it just before the alloc (after all failure returns, so a
  failed load never frees the existing signal).
- GenerateSpectrogramTexture overwrote *image with a fresh GenImageColor
  without unloading the old one — leaked an image buffer on every reload
  and every dB-floor/colormap regen. Unload first (NULL-safe on first use).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-25 01:04:05 -07:00
parent ec5f77f6ef
commit ebe35bcd95
+3
View File
@@ -614,6 +614,7 @@ static void ApplyHannWindow(float* samples, int n)
static void ComputeSTFTInit(AudioSignal* signal, StftResult* result, int fftSize) static void ComputeSTFTInit(AudioSignal* signal, StftResult* result, int fftSize)
{ {
FreeSTFT(result); // release any previous result before reallocating
int hopSize = fftSize / HOP_RATIO; // 75% overlap int hopSize = fftSize / HOP_RATIO; // 75% overlap
int numSegments = (signal->numSamples - fftSize) / hopSize + 1; int numSegments = (signal->numSamples - fftSize) / hopSize + 1;
if (numSegments <= 0) numSegments = 1; if (numSegments <= 0) numSegments = 1;
@@ -913,6 +914,7 @@ static bool LoadWavFile(const char* filepath, AudioSignal* signal)
signal->channels = wave.channels; signal->channels = wave.channels;
signal->numSamples = wave.frameCount * wave.channels; signal->numSamples = wave.frameCount * wave.channels;
signal->duration = (float)wave.frameCount / wave.sampleRate; signal->duration = (float)wave.frameCount / wave.sampleRate;
if (signal->samples) free(signal->samples); // free previous file's samples
signal->samples = (float*)malloc(signal->numSamples * sizeof(float)); signal->samples = (float*)malloc(signal->numSamples * sizeof(float));
if (wave.sampleSize == 16) { if (wave.sampleSize == 16) {
@@ -965,6 +967,7 @@ static void GenerateSpectrogramTexture(StftResult* stft, Image* image, Texture2D
int fftSize = (height - 1) * 2; int fftSize = (height - 1) * 2;
float freqPerBin = (float)stft->sampleRate / fftSize; float freqPerBin = (float)stft->sampleRate / fftSize;
UnloadImage(*image); // release previous image (NULL-safe on first call)
*image = GenImageColor(width, height, BLACK); *image = GenImageColor(width, height, BLACK);
Color* pixels = (Color*)image->data; Color* pixels = (Color*)image->data;