]> git.djapps.eu Git - pkg/ggml/sources/whisper.cpp/commitdiff
whisper : fix potential memory leaks (#740)
authorBader-eddine Ouaich <redacted>
Fri, 14 Apr 2023 17:05:56 +0000 (17:05 +0000)
committerGitHub <redacted>
Fri, 14 Apr 2023 17:05:56 +0000 (20:05 +0300)
* fix potential memory leak if whisper_init_state failed

* fix potential memory leak if gpt2_init failed

examples/talk.wasm/gpt-2.cpp
whisper.cpp

index bc5e099b47f794deb19443b41145631e719f679b..9f5b796989cb7cd8a71c5711b55435e5ef0ee2f0 100644 (file)
@@ -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;
         }
 
index 3c9bdc45402b87e592703b86efc47c509e0daf17..1e69da059ce9973ced4128092b85c7a972b89b3d 100644 (file)
@@ -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;
     }