Skip to content

Commit 6e0438d

Browse files
authored
gguf : fix resource leaks (#6061)
There several places where a gguf context is allocated. A call to gguf_free is missing in some error paths. Also on linux, llama-bench was missing a fclose.
1 parent 7271077 commit 6e0438d

File tree

4 files changed

+7
-0
lines changed

4 files changed

+7
-0
lines changed

examples/gguf/gguf.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ static bool gguf_ex_read_1(const std::string & fname) {
211211
for (int j = 0; j < ggml_nelements(cur); ++j) {
212212
if (data[j] != 100 + i) {
213213
fprintf(stderr, "%s: tensor[%d]: data[%d] = %f\n", __func__, i, j, data[j]);
214+
gguf_free(ctx);
214215
return false;
215216
}
216217
}

examples/llama-bench/llama-bench.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ static std::string get_cpu_info() {
103103
}
104104
}
105105
}
106+
fclose(f);
106107
}
107108
#endif
108109
// TODO: other platforms

examples/llava/clip.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,13 +995,15 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) {
995995
if (!new_clip->ctx_data) {
996996
fprintf(stderr, "%s: ggml_init() failed\n", __func__);
997997
clip_free(new_clip);
998+
gguf_free(ctx);
998999
return nullptr;
9991000
}
10001001

10011002
auto fin = std::ifstream(fname, std::ios::binary);
10021003
if (!fin) {
10031004
printf("cannot open model file for loading tensors\n");
10041005
clip_free(new_clip);
1006+
gguf_free(ctx);
10051007
return nullptr;
10061008
}
10071009

@@ -1023,6 +1025,7 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) {
10231025
if (!fin) {
10241026
printf("%s: failed to seek for tensor %s\n", __func__, name);
10251027
clip_free(new_clip);
1028+
gguf_free(ctx);
10261029
return nullptr;
10271030
}
10281031
int num_bytes = ggml_nbytes(cur);
@@ -1908,6 +1911,7 @@ bool clip_model_quantize(const char * fname_inp, const char * fname_out, const i
19081911
break;
19091912
default:
19101913
printf("Please use an input file in f32 or f16\n");
1914+
gguf_free(ctx_out);
19111915
return false;
19121916
}
19131917

examples/train-text-from-scratch/train-text-from-scratch.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,7 @@ static bool load_checkpoint_file(const char * filename, struct my_llama_model *
711711

712712
load_checkpoint_gguf(fctx, f_ggml_ctx, model, train);
713713

714+
gguf_free(fctx);
714715
return true;
715716
}
716717

0 commit comments

Comments
 (0)