From 2c856fb9e547264cc556c0cf2f42c6bc68c1426f Mon Sep 17 00:00:00 2001 From: Bader-eddine Ouaich <49657842+baderouaich@users.noreply.github.com> Date: Fri, 14 Apr 2023 17:05:56 +0000 Subject: [PATCH] whisper : fix potential memory leaks (#740) * fix potential memory leak if whisper_init_state failed * fix potential memory leak if gpt2_init failed --- examples/talk.wasm/gpt-2.cpp | 1 + whisper.cpp | 2 ++ 2 files changed, 3 insertions(+) diff --git a/examples/talk.wasm/gpt-2.cpp b/examples/talk.wasm/gpt-2.cpp index bc5e099..9f5b796 100644 --- a/examples/talk.wasm/gpt-2.cpp +++ b/examples/talk.wasm/gpt-2.cpp @@ -841,6 +841,7 @@ struct gpt2_context * gpt2_init(const char * path_model) { if (!gpt2_model_load(path_model, ctx->model, ctx->vocab)) { fprintf(stderr, "%s: failed to load model from '%s'\n", __func__, "gpt-2.bin"); + delete ctx; return nullptr; } diff --git a/whisper.cpp b/whisper.cpp index 3c9bdc4..1e69da0 100644 --- a/whisper.cpp +++ b/whisper.cpp @@ -2487,6 +2487,7 @@ struct whisper_state * whisper_init_state(whisper_context * ctx) { if (!kv_cache_init(ctx->model.hparams, scale * MEM_REQ_KV_SELF.at(ctx->model.type), state->decoders[0].kv_self, ctx->wtype, ctx->model.hparams.n_text_ctx)) { fprintf(stderr, "%s: kv_cache_init() failed for self-attention cache\n", __func__); + delete state; return nullptr; } @@ -2497,6 +2498,7 @@ struct whisper_state * whisper_init_state(whisper_context * ctx) { if (!kv_cache_init(ctx->model.hparams, scale * MEM_REQ_KV_CROSS.at(ctx->model.type), state->kv_cross, ctx->wtype, ctx->model.hparams.n_audio_ctx)) { fprintf(stderr, "%s: kv_cache_init() failed for cross-attention cache\n", __func__); + delete state; return nullptr; }