问题描述
这是 Valgrind 的输出:
Here is the output from 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)
对于地图中的每个条目都会重复相同的错误.
This same error is repeated for every entry in the map.
main.cpp 第 32 行是:
main.cpp line 32 is:
pilInpOpts input;
pilInpOpts 的第 12 行是构造函数的一部分:
Line 12 of the pilInpOpts is part of the constructor:
#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 内存泄漏
I found some posts in SO which said Valgrind may produce false positives. For example: std::string memory leak
这是一个误报,因为 std::string 有它需要的所有构造函数等吗?或者我应该更改为在地图中使用 C 字符数组?
Is this a false positive since std::string has all the constructors etc it needs to do this? Or should I change to use C character arrays in the map?
推荐答案
这种行为的一个可能原因可能是 C++ 标准库实现中的内存池.
One of the probable reasons of this behavior may be memory pooling in C++ standard library implementation.
来自 Valgrind 常见问题:
很多被破坏的对象的内存不是立即的释放并返回给操作系统,但保留在池中以备后用重复使用.池在出口处未释放的事实程序导致 Valgrind 报告此内存仍可访问.这在出口处不释放池的行为可以称为图书馆.
您可以在运行您的应用之前设置 GLIBCXX_FORCE_NEW
环境变量以强制 STL 尽快释放内存.
You can set GLIBCXX_FORCE_NEW
environment variable before running your app to force STL to free memory as soon as possible.
另见有关 libstdc++ 内存分配器实现细节的这些链接:
See also these links on details of libstdc++ memory allocator implementation:
- https://gcc.gnu.org/onlinedocs/libstdc++/faq.html#faq.memory_leaks
- https://gcc.gnu.org/onlinedocs/libstdc++/manual/debug.html#debug.memory
这篇关于Valgrind 内存泄漏与 std::map 中的 std::string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!