From ebe35bcd95ce6e0e6ec203939f3e146649854760 Mon Sep 17 00:00:00 2001 From: Tyler Date: Mon, 25 May 2026 01:04:05 -0700 Subject: [PATCH] fix: plug memory leaks on file reload and texture regen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/spectrogram.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/spectrogram.c b/src/spectrogram.c index 65c2c6d..bfe1dd7 100644 --- a/src/spectrogram.c +++ b/src/spectrogram.c @@ -614,6 +614,7 @@ static void ApplyHannWindow(float* samples, int n) 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 numSegments = (signal->numSamples - fftSize) / hopSize + 1; if (numSegments <= 0) numSegments = 1; @@ -913,6 +914,7 @@ static bool LoadWavFile(const char* filepath, AudioSignal* signal) signal->channels = wave.channels; signal->numSamples = wave.frameCount * wave.channels; 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)); if (wave.sampleSize == 16) { @@ -965,6 +967,7 @@ static void GenerateSpectrogramTexture(StftResult* stft, Image* image, Texture2D int fftSize = (height - 1) * 2; float freqPerBin = (float)stft->sampleRate / fftSize; + UnloadImage(*image); // release previous image (NULL-safe on first call) *image = GenImageColor(width, height, BLACK); Color* pixels = (Color*)image->data;