mirror of
				https://github.com/ggml-org/llama.cpp.git
				synced 2025-10-30 08:42:00 +00:00 
			
		
		
		
	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.
This commit is contained in:
		| @@ -211,6 +211,7 @@ static bool gguf_ex_read_1(const std::string & fname) { | |||||||
|                 for (int j = 0; j < ggml_nelements(cur); ++j) { |                 for (int j = 0; j < ggml_nelements(cur); ++j) { | ||||||
|                     if (data[j] != 100 + i) { |                     if (data[j] != 100 + i) { | ||||||
|                         fprintf(stderr, "%s: tensor[%d]: data[%d] = %f\n", __func__, i, j, data[j]); |                         fprintf(stderr, "%s: tensor[%d]: data[%d] = %f\n", __func__, i, j, data[j]); | ||||||
|  |                         gguf_free(ctx); | ||||||
|                         return false; |                         return false; | ||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|   | |||||||
| @@ -103,6 +103,7 @@ static std::string get_cpu_info() { | |||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |         fclose(f); | ||||||
|     } |     } | ||||||
| #endif | #endif | ||||||
|     // TODO: other platforms |     // TODO: other platforms | ||||||
|   | |||||||
| @@ -995,6 +995,7 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) { | |||||||
|         if (!new_clip->ctx_data) { |         if (!new_clip->ctx_data) { | ||||||
|             fprintf(stderr, "%s: ggml_init() failed\n", __func__); |             fprintf(stderr, "%s: ggml_init() failed\n", __func__); | ||||||
|             clip_free(new_clip); |             clip_free(new_clip); | ||||||
|  |             gguf_free(ctx); | ||||||
|             return nullptr; |             return nullptr; | ||||||
|         } |         } | ||||||
|  |  | ||||||
| @@ -1002,6 +1003,7 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) { | |||||||
|         if (!fin) { |         if (!fin) { | ||||||
|             printf("cannot open model file for loading tensors\n"); |             printf("cannot open model file for loading tensors\n"); | ||||||
|             clip_free(new_clip); |             clip_free(new_clip); | ||||||
|  |             gguf_free(ctx); | ||||||
|             return nullptr; |             return nullptr; | ||||||
|         } |         } | ||||||
|  |  | ||||||
| @@ -1023,6 +1025,7 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) { | |||||||
|             if (!fin) { |             if (!fin) { | ||||||
|                 printf("%s: failed to seek for tensor %s\n", __func__, name); |                 printf("%s: failed to seek for tensor %s\n", __func__, name); | ||||||
|                 clip_free(new_clip); |                 clip_free(new_clip); | ||||||
|  |                 gguf_free(ctx); | ||||||
|                 return nullptr; |                 return nullptr; | ||||||
|             } |             } | ||||||
|             int num_bytes = ggml_nbytes(cur); |             int num_bytes = ggml_nbytes(cur); | ||||||
| @@ -1908,6 +1911,7 @@ bool clip_model_quantize(const char * fname_inp, const char * fname_out, const i | |||||||
|                 break; |                 break; | ||||||
|             default: |             default: | ||||||
|                 printf("Please use an input file in f32 or f16\n"); |                 printf("Please use an input file in f32 or f16\n"); | ||||||
|  |                 gguf_free(ctx_out); | ||||||
|                 return false; |                 return false; | ||||||
|             } |             } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -711,6 +711,7 @@ static bool load_checkpoint_file(const char * filename, struct my_llama_model * | |||||||
|  |  | ||||||
|     load_checkpoint_gguf(fctx, f_ggml_ctx, model, train); |     load_checkpoint_gguf(fctx, f_ggml_ctx, model, train); | ||||||
|  |  | ||||||
|  |     gguf_free(fctx); | ||||||
|     return true; |     return true; | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Steve Grubb
					Steve Grubb