boost program_options生成一个Klocwork MLK.MUST [英] boost program_options generating a Klocwork MLK.MUST
问题描述
使用简单的示例,Klocwork洞察力检测到:
using a straightforward example, Klocwork insight detects:
namespace po = boost::program_options;
po::options_description oArgDesc("Options");
oArgDesc.add_options()
("sN", po::value<vector<string>>()->required()->multitoken(), "List of destination names.")
("sV", po::value<vector<string>>()->required()->multitoken(), "List of source names.")
;
Klocwork消息: 内存泄漏.通过函数'value,std :: allocator>,std :: allocator,std :: allocator>>>>'在第27行分配的'po :: value>()'中存储的动态内存在第26行丢失.是第26行的一个类似错误.
Klocwork message: Memory leak. Dynamic memory stored in 'po::value > ()' allocated through function 'value,std::allocator >,std::allocator,std::allocator > > > >' at line 27 is lost at line 26. Also there is one similar error on line 26.
单步执行:value_semantic.hpp,靠近第185行(提升1.54),我看到了new():
Single-step inside: value_semantic.hpp, near line 185 (boost 1.54), I see new():
typed_value<T>* r = new typed_value<T>(v);
在内部查看:options_description.hpp,在第70行附近,我看到一个空的析构函数
Looking inside: options_description.hpp, near line 70, I see an empty destructor
option_description::~option_description()
{
}
在 boost \ boost_1_54_0 \ boost \ program_options \ detail * hpp文件.
I do not see an obvious location where a delete is called, in the boost\boost_1_54_0\boost\program_options\detail*hpp files.
到目前为止,我还没有尝试使用其他内存分析工具(例如,purify).
I have not tried another memory analysis tool (e.g., purify) as of yet either.
推荐答案
value_semantic
最终将由boost::shared_ptr
管理.如果在new
,boost::shared_ptr
的构造函数或boost::program_options::option_description
的构造函数中引发异常,则value_semantic
将泄漏.
The value_semantic
eventually becomes managed by a boost::shared_ptr
. The value_semantic
will leak if an exception is thrown in new
, boost::shared_ptr
's constructor, or boost::program_options::option_description
's constructor.
使用以下代码:
namespace po = boost::program_options;
po::options_description desc("Options");
desc.add_options()
("name", po::value<std::string>(), ...) // add option
;
boost::program_options::value()
将在免费存储区中分配一个value_semantic
.当作为选项添加时,value_semantic
将传递到新创建的option_description
,由构造函数中,shared_ptr
用于初始化
boost::program_options::value()
will allocate a value_semantic
in the free store. When added as an option, the value_semantic
is passed to a newly created option_description
, managed by a shared_ptr
within options_description_easy_init::operator()
. Within option_description
's constructor, the value_semantic
becomes managed by a shared_ptr
when it used to initialize the m_value_semantic
member variable.
这篇关于boost program_options生成一个Klocwork MLK.MUST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!