[英]What happens to globally allocated memory?
I have a program like this: 我有一个这样的程序:
int *number0 = new int;
int main()
{
int *number1 = new int;
}
I thought, both memory allocations would introduce memory leaks, though valgrind only complains about number1 inside the main function. 我想,两个内存分配都会引入内存泄漏,虽然valgrind只会在main函数中抱怨number1。 Why is that?
这是为什么?
Valgrind is not a perfect tool for detecting the presence or absence of all possible memory leaks, but rather a helpful tool that can detect some memory leaks. Valgrind不是检测是否存在所有可能的内存泄漏的完美工具,而是一种可以检测到一些内存泄漏的有用工具。 This means that valgrind output cannot be used to determine whether a particular piece of code contains or does not contain any leaks.
这意味着valgrind输出不能用于确定特定代码段是否包含任何泄漏。
Neither of your new
s have corresponding delete
s, and in that sense, they are both leaked. 你的
new
都没有相应的delete
,从这个意义上说,他们都被泄露了。
It is likely that valgrind considers the number0
memory to not be leaked because the memory it points to is reachable at the end of the program execution. 很可能valgrind认为
number0
内存不会被泄露,因为它指向的内存在程序执行结束时是可以访问的。 In contrast to this, number1
goes out of scope, and so the memory it points to is not reachable at the end of the execution of your program, and so valgrind considers it to be leaked. 与此相反,
number1
超出了范围,因此它指向的内存在程序执行结束时无法访问,因此valgrind认为它被泄露。
Running this 运行这个
int *x = new int;
int main()
{
return 0;
}
code (ie without the leak in the main, compiled with g++ 4.8.1) using valgrind (3.8.1) with ( -v --track-origins=yes --leak-check=full --show-reachable=yes
) I get: 使用valgrind(3.8.1)与(
-v --track-origins=yes --leak-check=full --show-reachable=yes
)使用valgrind(3.8.1)编译(即没有主要的泄漏,用g ++ 4.8.1编译)我明白了:
==34301==
==34301== HEAP SUMMARY:
==34301== in use at exit: 4 bytes in 1 blocks
==34301== total heap usage: 1 allocs, 0 frees, 4 bytes allocated
==34301==
==34301== Searching for pointers to 1 not-freed blocks
==34301== Checked 189,064 bytes
==34301==
==34301== 4 bytes in 1 blocks are still reachable in loss record 1 of 1
==34301== at 0x4C2A879: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==34301==
==34301== LEAK SUMMARY:
==34301== definitely lost: 0 bytes in 0 blocks
==34301== indirectly lost: 0 bytes in 0 blocks
==34301== possibly lost: 0 bytes in 0 blocks
==34301== still reachable: 4 bytes in 1 blocks
==34301== suppressed: 0 bytes in 0 blocks
Which means that you should also pay attention to the in use at exit
category. 这意味着您还应该注意
in use at exit
类别in use at exit
。
It doesn't seem like valgrind missed it, just placed it in another category, possibly because they assume something as lost only when you cannot by any means track down that address and release it, but this variable is never lost . 看起来valgrind似乎没有错过它,只是把它放在另一个类别中,可能是因为只有当你无法通过任何方式追踪该地址并释放它时它们才会假设丢失的东西,但这个变量永远不会丢失 。
This however : 但是这样:
int *x = new int;
int main()
{
x = new int;
return 0;
}
Is detected as a leak as you really lose track of the memory you've allocated. 被检测为泄漏,你真的失去跟踪你分配的内存中。
EDIT: As described in the Mem-check manual : 编辑:如记忆检查手册中所述 :
"Still reachable".
“仍然可以到达”。 This covers cases 1 and 2 (for the BBB blocks) above.
这包括上面的情况1和2(对于BBB块)。 A start-pointer or chain of start-pointers to the block is found.
找到该块的起始指针或起始指针链。 Since the block is still pointed at, the programmer could, at least in principle, have freed it before program exit.
由于仍然指向该块,程序员至少在原则上可以在程序退出之前释放它。 "Still reachable" blocks are very common and arguably not a problem.
“仍然可以访问”的块是非常常见的,可以说不是问题。 So, by default, Memcheck won't report such blocks individually.
因此,默认情况下,Memcheck不会单独报告此类块。
So as noted before they do detect it, they just think it's less exciting 因此,如前所述他们确实发现了它,他们只是认为它不那么令人兴奋
I see two leaks using valgrind 3.8.1 / g++ 4.8.1: 我使用valgrind 3.8.1 / g ++ 4.8.1看到两个泄漏:
$ g++ -o foo -g foo.cc
$ valgrind ./foo
...
==7789== HEAP SUMMARY:
==7789== in use at exit: 8 bytes in 2 blocks
==7789== total heap usage: 2 allocs, 0 frees, 8 bytes allocated
==7789==
==7789== LEAK SUMMARY:
==7789== definitely lost: 4 bytes in 1 blocks
==7789== indirectly lost: 0 bytes in 0 blocks
==7789== possibly lost: 0 bytes in 0 blocks
==7789== still reachable: 4 bytes in 1 blocks
==7789== suppressed: 0 bytes in 0 blocks
The "definitely lost" bytes are in main
in my test. 在我的测试中,“绝对丢失”的字节是
main
。
This 这个
int *number0 = new int;
is not a memory leak because it gets reclaimed at the end of execution. 不是内存泄漏,因为它在执行结束时被回收。
This configuration is a [potential] memory leak because 这种配置是[潜在的]内存泄漏,因为
int main()
{
int *number1 = new int;
}
some other part of the code could call 代码的其他部分可以调用
main () ;
and it could be called repeatedly. 它可以被反复调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.