]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
llava: improve clip_ctx destructor to not memleak load_image_size (#12834)
authorMatt Clayton <redacted>
Tue, 8 Apr 2025 20:01:58 +0000 (16:01 -0400)
committerGitHub <redacted>
Tue, 8 Apr 2025 20:01:58 +0000 (22:01 +0200)
examples/llava/clip.cpp
examples/llava/clip.h

index e9520f3d1a3784e95d61cbaf1439a82d76781ef6..44428cc959553618b1cc62facb2789e3f264021c 100644 (file)
@@ -380,6 +380,7 @@ struct clip_ctx {
         if (backend_cpu != backend) {
             ggml_backend_free(backend_cpu);
         }
+        clip_image_size_free(load_image_size);
     }
 };
 
@@ -1618,6 +1619,12 @@ struct clip_image_f32 * clip_image_f32_init() {
     return new clip_image_f32();
 }
 
+void clip_image_size_free(struct clip_image_size * load_image_size) {
+    if (load_image_size == nullptr) {
+        return;
+    }
+    delete load_image_size;
+}
 void clip_image_u8_free(struct clip_image_u8  * img) { delete img; }
 void clip_image_f32_free(struct clip_image_f32 * img) { delete img; }
 void clip_image_u8_batch_free(struct clip_image_u8_batch  * batch) {
@@ -2270,6 +2277,9 @@ ggml_tensor * clip_get_newline_tensor(const struct clip_ctx * ctx) {
 }
 
 void clip_free(clip_ctx * ctx) {
+    if (ctx == nullptr) {
+        return;
+    }
     delete ctx;
 }
 
index d806465bf68bb66af4e9036f959fd0c28e9901fe..87aa61574b1eb86c1241346d3f0bea78aecc3d6b 100644 (file)
@@ -77,6 +77,7 @@ CLIP_API struct clip_image_size * clip_image_size_init();
 CLIP_API struct clip_image_u8  * clip_image_u8_init ();
 CLIP_API struct clip_image_f32 * clip_image_f32_init();
 
+CLIP_API void clip_image_size_free (struct clip_image_size * img_size);
 CLIP_API void clip_image_u8_free (struct clip_image_u8  * img);
 CLIP_API void clip_image_f32_free(struct clip_image_f32 * img);
 CLIP_API void clip_image_u8_batch_free (struct clip_image_u8_batch  * batch);