Skip to content

Commit 088e00b

Browse files
common: fix memory leak in tensor override parser
1 parent 8960efd commit 088e00b

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

common/arg.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2344,8 +2344,9 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
23442344
}
23452345
throw std::invalid_argument("unknown buffer type");
23462346
}
2347-
// FIXME: this leaks memory
2348-
params.tensor_buft_overrides.push_back({strdup(tensor_name.c_str()), buft_list.at(buffer_type)});
2347+
// store pattern to ensure lifetime for the C-string
2348+
params.tensor_buft_override_names.push_back(tensor_name);
2349+
params.tensor_buft_overrides.push_back({params.tensor_buft_override_names.back().c_str(), buft_list.at(buffer_type)});
23492350
}
23502351
}
23512352
));

common/common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <string>
99
#include <string_view>
1010
#include <vector>
11+
#include <deque>
1112
#include <sstream>
1213

1314
#ifdef _WIN32
@@ -281,6 +282,7 @@ struct common_params {
281282
std::vector<std::string> in_files; // all input files
282283
std::vector<std::string> antiprompt; // strings upon which more user input is prompted (a.k.a. reverse prompts)
283284
std::vector<llama_model_kv_override> kv_overrides;
285+
std::deque<std::string> tensor_buft_override_names; // storage for tensor override patterns
284286
std::vector<llama_model_tensor_buft_override> tensor_buft_overrides;
285287

286288
bool lora_init_without_apply = false; // only load lora to memory, but do not apply it to ctx (user can manually apply lora later using llama_adapter_lora_apply)

0 commit comments

Comments
 (0)