boost program_options generating a Klocwork MLK.MUST

一曲冷凌霜 提交于 2020-08-07 10:34:10

问题


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 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.

Single-step inside: value_semantic.hpp, near line 185 (boost 1.54), I see new():

typed_value<T>* r = new typed_value<T>(v);

Looking inside: options_description.hpp, near line 70, I see an empty destructor

option_description::~option_description()
{
}

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.

I have not tried another memory analysis tool (e.g., purify) as of yet either.


回答1:


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.

With the following code:

namespace po = boost::program_options;
po::options_description desc("Options");
desc.add_options()
  ("name", po::value<std::string>(), ...) // add option
  ;

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.



来源:https://stackoverflow.com/questions/18750691/boost-program-options-generating-a-klocwork-mlk-must

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!