Skip to content

Commit f990121

Browse files
mattjclyjwcolin
authored andcommitted
llava: improve clip_ctx destructor to not memleak load_image_size (ggml-org#12834)
1 parent fcbcb5c commit f990121

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

examples/llava/clip.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ struct clip_ctx {
380380
if (backend_cpu != backend) {
381381
ggml_backend_free(backend_cpu);
382382
}
383+
clip_image_size_free(load_image_size);
383384
}
384385
};
385386

@@ -1618,6 +1619,12 @@ struct clip_image_f32 * clip_image_f32_init() {
16181619
return new clip_image_f32();
16191620
}
16201621

1622+
void clip_image_size_free(struct clip_image_size * load_image_size) {
1623+
if (load_image_size == nullptr) {
1624+
return;
1625+
}
1626+
delete load_image_size;
1627+
}
16211628
void clip_image_u8_free(struct clip_image_u8 * img) { delete img; }
16221629
void clip_image_f32_free(struct clip_image_f32 * img) { delete img; }
16231630
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) {
22702277
}
22712278

22722279
void clip_free(clip_ctx * ctx) {
2280+
if (ctx == nullptr) {
2281+
return;
2282+
}
22732283
delete ctx;
22742284
}
22752285

examples/llava/clip.h

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ CLIP_API struct clip_image_size * clip_image_size_init();
7777
CLIP_API struct clip_image_u8 * clip_image_u8_init ();
7878
CLIP_API struct clip_image_f32 * clip_image_f32_init();
7979

80+
CLIP_API void clip_image_size_free (struct clip_image_size * img_size);
8081
CLIP_API void clip_image_u8_free (struct clip_image_u8 * img);
8182
CLIP_API void clip_image_f32_free(struct clip_image_f32 * img);
8283
CLIP_API void clip_image_u8_batch_free (struct clip_image_u8_batch * batch);

0 commit comments

Comments
 (0)