为什么在工厂查询表上显示“字段指示的潜在内存泄漏”

时间:2019-04-15 07:03:08

标签: memory-leaks c++17 static-analysis

我正在尝试实现一个工厂,该工厂创建由公共基类 Field 继承的类型的新实例。

将工厂建模为工厂函数的查找表,该函数返回unique_ptr。查找表的键类型是string_view。

在工厂函数中,我为请求的类型调用make_unique。

clang-analyzer抱怨在unique_ptr返回时可能发生内存泄漏。

带有单个可用工厂条目的工厂查找表。

  using FieldFactory = std::function< std::unique_ptr< Field >( node const&, Clf const& ) >;

  static const std::unordered_map< std::string_view, FieldFactory > fieldFactoryLut = {
    {"static-text"sv, []( auto const& node, auto const& parent ) { return std::make_unique< StaticTextField >( node, parent ); }}
  };

用法

auto const& factoryFun = fieldFactoryLut.at("static-text"sv);
auto uPtr = factoryFun(node, clf);

通过调用make_unique,我希望不会发生内存泄漏,但是clang-analyzer声称:

warning: Potential leak of memory pointed to by field '_M_head_impl' [clang-analyzer-cplusplus.NewDeleteLeaks]
[build]     {"static-text"sv, []( auto const& node, auto const& parent ) { return std::make_unique< StaticTextField >( node, parent ); }}

谁能告诉我在这里看到的叮当声?

0 个答案:

没有答案
相关问题