gpt4 book ai didi

c++ - std::map 中的 std::string 导致 Valgrind 内存泄漏

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:36:25 24 4
gpt4 key购买 nike

这是 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)

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

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 memory leak

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

最佳答案

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

来自 Valgrind faq :

Memory for quite a number of destructed objects is not immediately freed and given back to the OS, but kept in the pool(s) for later re-use. The fact that the pools are not freed at the exit of the program cause Valgrind to report this memory as still reachable. The behaviour not to free pools at the exit could be called a bug of the library though.

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

另请参阅以下有关 libstdc++ 内存分配器实现细节的链接:

关于c++ - std::map 中的 std::string 导致 Valgrind 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24457098/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com