c++ valgrind在hello world中显示内存泄漏

标签 c++ memory-leaks standard-library

<分区>

我的程序代码是

#include <iostream>

int main(int argc, const char *argv[])
{
  std::cout << "hello world!\n";
  return 0;
}

我是用flags编译的

-Wpedantic -pedantic-errors -std=c++11 -g -Wall -Wextra

在上面运行 Valgrind,看到一些奇怪的东西,这个简单的程序有内存泄漏,valgrind --leak-check=full --show-leak-kinds=all 命令的输出是

==4492== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
==4492==    at 0x4C28C20: malloc (vg_replace_malloc.c:296)
==4492==    by 0x4EBF11F: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==4492==    by 0x400E9F9: call_init.part.0 (dl-init.c:78)
==4492==    by 0x400EAE2: call_init (dl-init.c:36)
==4492==    by 0x400EAE2: _dl_init (dl-init.c:126)
==4492==    by 0x40011C9: ??? (in /lib/x86_64-linux-gnu/ld-2.19.so)

我的问题是 - 如何找出正在发生的事情?

最佳答案

这是linux系统动态库加载器永久保留的内存。找出发生了什么的方法包括阅读 _dl_init() 函数的代码,例如:here .另一种选择是使用调试器单步执行程序,您需要在运行前break _init 并且可能还使用disassemblesi,因为 glibc 不能在未优化的情况下构建。

参见讨论 here (并可能标记为重复)

关于c++ valgrind在hello world中显示内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35871265/

相关文章:

c++ - OpenMP #pragma,只有一个线程在处理我的代码

html5 canvas 应用程序中的 JavaScript base64 图像源内存管理

c - strlen 和 malloc : C memory leaks

C++ 的 std::string 池,调试版本? std::string 和 valgrind 问题

c - div() 库函数的用途是什么?

c++ - 仿函数编译时错误

c++ - 我应该在我的 C++ std 随机分布上调用 reset() 来清除隐藏状态吗?

c++ - 乘法类型的确定

c++ - 如何在 C++ 中将字符串解析为多种类型?

c++ - C++中的异常处理问题