std::map 中 std::string 的 Valgrind 内存泄漏

2024-03-25

这是 Valgrind 的输出:

==6519==    at 0x4C25885: operator new(unsigned long) (vg_replace_malloc.c:319)
==6519==    by 0x4EE65D8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (new_allocator.h:104)
==6519==    by 0x4EE7CE0: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (basic_string.tcc:138)
==6519==    by 0x4EE80F7: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (basic_string.h:1725)
==6519==    by 0x41C399: pilInpOpts::pilInpOpts() (pilInpOpts.cpp:12)
==6519==    by 0x403A55: main (main.cpp:32)

地图中的每个条目都会重复同样的错误。

main.cpp第32行是:

    pilInpOpts input;

pilInpOpts 的第 12 行是构造函数的一部分:

#include "pilInpOpts.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>


pilInpOpts::pilInpOpts() 
{
// create the map of options, put in alphabetical order to ease sorting
piloptmap.insert(std::pair<std::string, bool>("bforce",false));
piloptmap.insert(std::pair<std::string, bool>("coef",false));
piloptmap.insert(std::pair<std::string, bool>("dualjet",false));
piloptmap.insert(std::pair<std::string, bool>("flow",false));
piloptmap.insert(std::pair<std::string, bool>("gforce",false));
piloptmap.insert(std::pair<std::string, bool>("gpress",false));
piloptmap.insert(std::pair<std::string, bool>("matlab",false));
piloptmap.insert(std::pair<std::string, bool>("model",false));
piloptmap.insert(std::pair<std::string, bool>("out_shade",false));
piloptmap.insert(std::pair<std::string, bool>("out_shade_file",false));
piloptmap.insert(std::pair<std::string, bool>("press",false));
piloptmap.insert(std::pair<std::string, bool>("proc",false));
piloptmap.insert(std::pair<std::string, bool>("shade",false));
piloptmap.insert(std::pair<std::string, bool>("summary",false));
piloptmap.insert(std::pair<std::string, bool>("trans",false));
// need to define the default filepaths, this is needed because they are optional
platpath = "";
vehpath = "";
apppath = "";
dockpath = "";
};

我在 SO 中发现一些帖子说 Valgrind 可能会产生误报。例如:std::string 内存泄漏 https://stackoverflow.com/questions/13001784/c-stdstring-memory-leak

这是误报吗,因为 std::string 具有执行此操作所需的所有构造函数等?或者我应该更改为在地图中使用 C 字符数组?


此行为的可能原因之一可能是 C++ 标准库实现中的内存池。

From Valgrind 常见问题解答 http://valgrind.org/docs/manual/faq.html#faq.reports:

相当多的被破坏的对象的内存不是立即被破坏的 释放并返回给操作系统,但保留在池中供以后使用 重复使用。事实上,池在出口处没有被释放 程序导致 Valgrind 报告该内存仍可访问。这 在出口处不释放池的行为可以称为错误 不过图书馆。

您可以设置GLIBCXX_FORCE_NEW运行应用程序之前的环境变量,以强制 STL 尽快释放内存。

另请参阅以下链接,了解 libstdc++ 内存分配器实现的详细信息:

  • https://gcc.gnu.org/onlinedocs/libstdc++/faq.html#faq.memory_leaks https://gcc.gnu.org/onlinedocs/libstdc++/faq.html#faq.memory_leaks
  • https://gcc.gnu.org/onlinedocs/libstdc++/manual/debug.html#debug.memory https://gcc.gnu.org/onlinedocs/libstdc++/manual/debug.html#debug.memory
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

std::map 中 std::string 的 Valgrind 内存泄漏 的相关文章

随机推荐