我们有一个现有的C项目,我们正在使用vowpal wabbit。我们的用例是如果模型目录中更新的新模型重新加载模型,但每次初始化后我们都会遇到内存泄漏(在valgrind中)。
内存泄漏堆栈跟踪(来自valgrind)和代码剪切在下面。
==31962== 7,093,114 (104 direct, 7,093,010 indirect) bytes in 1 blocks are definitely lost in loss record 1,655 of 1,660
==31962== at 0x4A075FC: operator new(unsigned long) (vg_replace_malloc.c:298)
==31962== by 0x4F59F4D: boost::program_options::options_description::add(boost::program_options::options_description const&) (in /usr/lib64/libboost_program_options.so.1.59.0)
==31962== by 0x5AAB82: add_options(vw&) (in testDebug)
==31962== by 0x5BC95E: parse_source(vw&) (in testDebug)
==31962== by 0x5BD877: parse_sources(vw&, io_buf&, bool) (in testDebug)
==31962== by 0x5BE23D: VW::initialize(int, char**, io_buf*, bool, void (*)(void*, std::string const&), void*) (in testDebug)
==31962== by 0x5BE4FA: VW::initialize(std::string, io_buf*, bool, void (*)(void*, std::string const&), void*) (in testDebug)
==31962== by 0x53D658: load_vw_model_from_file (vw_predict_model.cpp:296)
==31962== by 0x30858077F0: start_thread (in /lib64/libpthread-2.12.so)
==31962== by 0x30854E570C: clone (in /lib64/libc-2.12.so
我的代码剪断了 -
//Declaration
vw* model;
pthread_rwlock_t vw_pmodel_rwlock = PTHREAD_RWLOCK_INITIALIZER;
//getting prediction
double predict_vw_probability(string vw_string) {
double prob=-1.0;
if (vw_string.length() > 0) {
pthread_rwlock_rdlock(&vw_pmodel_rwlock);
example *vec2 = VW::read_example(*model,vw_string);
model->learn(vec2);
prob = vec2->pred.prob;
VW::finish_example(*model, vec2);
pthread_rwlock_unlock(&vw_pmodel_rwlock);
}
return prob;
}
void load_vw_model_from_file(const char* model_file_path) {
pthread_rwlock_wrlock(&vw_pmodel_rwlock);
//destroying old model
if (model != NULL) {
VW::finish(*model);
}
char *vw_model_tem;
asprintf(&vw_model_tem, "--link logistic --loss_function logistic -t -i %s", model_file_path);
std::string m_path(vw_model_tem);
model = VW::initialize(m_path);
if (NULL == model) {
//Error
}
pthread_rwlock_unlock(&vw_pmodel_rwlock);
free(vw_model_tem);
}
我们认为VW::finish(*model)
会处理内存释放,还是有其他办法?我们认为内存泄漏在libboost库中,请指导我们如何消除内存泄漏。