From: Daniel Bevenius Date: Mon, 6 Oct 2025 12:57:44 +0000 (+0200) Subject: vad : fix memory leaks in VAD implementation (#3453) X-Git-Tag: upstream/1.8.2~68 X-Git-Url: https://git.djapps.eu/?a=commitdiff_plain;h=c8223a8548ad64435266e551385fc51aca9ee8ab;p=pkg%2Fggml%2Fsources%2Fwhisper.cpp vad : fix memory leaks in VAD implementation (#3453) * vad : fix memory leak by storing ggml_context in vad context struct This commit addresses a memory leak issue in the voice activity detection (VAD) where the ggml_context is not stored within the vad context structure. The motivation for this change that this is causing the context memory to stay allocated and the tensor still point to that memory but this memory is never freed. * vad : free memory allocated for VAD hparams This commit frees the model hyperparameters allocated for the VAD context in the `whisper_vad_free` function. Specifically, it deletes the `encoder_in_channels`, `encoder_out_channels`, and `kernel_sizes` arrays allocated with `new[]` in the `whisper_vad_init` function. The motivation for this is to prevent memory leaks when the VAD. * vad: free ggml buffer in whisper_vad_free This commit frees the ggml buffer in the whisper_vad_free function to prevent memory leaks. Resolves: https://github.com/ggml-org/whisper.cpp/issues/3452 * Revert "vad : fix memory leak by storing ggml_context in vad context struct" This reverts commit aeafca437efa7fb28166703f845e321176aa62ab. * whisper : free ggml context in whisper_vad_init_context This commit frees the ggml_context after initializing the VAD context in the whisper_vad_init_context function. The motivation for this is to prevent memory leaks. --- diff --git a/src/whisper.cpp b/src/whisper.cpp index d99dd7be..39c53ba2 100644 --- a/src/whisper.cpp +++ b/src/whisper.cpp @@ -4676,6 +4676,7 @@ static bool whisper_vad_init_context(whisper_vad_context * vctx) { ggml_set_name(vctx->c_state, "c_state"); vctx->buffer = ggml_backend_alloc_ctx_tensors(ctx, vctx->backends[0]); + ggml_free(ctx); if (!vctx->buffer) { WHISPER_LOG_ERROR("%s: failed to allocate memory for the VAD state\n", __func__); return false; @@ -5420,6 +5421,9 @@ struct whisper_vad_segments * whisper_vad_segments_from_samples( void whisper_vad_free(whisper_vad_context * ctx) { if (ctx) { + if (ctx->buffer) { + ggml_backend_buffer_free(ctx->buffer); + } for (ggml_context * context : ctx->model.ctxs) { ggml_free(context); } @@ -5434,6 +5438,9 @@ void whisper_vad_free(whisper_vad_context * ctx) { ggml_backend_free(backend); } + delete[] ctx->model.hparams.encoder_in_channels; + delete[] ctx->model.hparams.encoder_out_channels; + delete[] ctx->model.hparams.kernel_sizes; delete ctx; }