[英]Understading Valgrind output
I wrote the following code to get a basic understanding of Valgrind and having a hard time interpreting its output. 我编写了以下代码,以基本了解Valgrind并难以解释其输出。 This probably is not related to Valgrind but more basic C++. 这可能与Valgrind不相关,但与更基本的C ++有关。
#include <string>
#include <iostream>
using namespace std;
class Valgrind_testclass
{
std::string * stringInHeap;
public:
Valgrind_testclass() {
stringInHeap = new std::string("String in heap");
}
~Valgrind_testclass() {
//delete stringInHeap;
}
void PrintFunc(void) {
cout << "Nothing but a printout" << endl;
}
};
int main()
{
Valgrind_testclass * valObjPtr = new Valgrind_testclass();
delete valObjPtr;
return 0;
}
Valgrind outputs: Valgrind输出:
==4459== HEAP SUMMARY:
==4459== in use at exit: 31 bytes in 2 blocks
==4459== total heap usage: 3 allocs, 1 frees, 35 bytes allocated
==4459==
==4459== Searching for pointers to 2 not-freed blocks
==4459== Checked 102,100 bytes
==4459==
==4459== 31 (4 direct, 27 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2
==4459== at 0x402641D: operator new(unsigned int) (vg_replace_malloc.c:255)
==4459== by 0x80487DB: Valgrind_testclass::Valgrind_testclass() (in /home/madu/C++/ValgrindTest)
==4459== by 0x80486F6: main (in /home/madu/C++/ValgrindTest)
==4459==
==4459== LEAK SUMMARY:
==4459== definitely lost: 4 bytes in 1 blocks
==4459== indirectly lost: 27 bytes in 1 blocks
==4459== possibly lost: 0 bytes in 0 blocks
==4459== still reachable: 0 bytes in 0 blocks
==4459== suppressed: 0 bytes in 0 blocks
==4459==
==4459== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 17 from 6)
Could someone please tell me where I do 3 allocations? 有人可以告诉我我在哪里进行3次分配吗? I can only see two allocations. 我只能看到两个分配。 Also why it says "indirectly lost"? 还为什么说“间接丢失”?
Thank you. 谢谢。
When you construct an std::string
object, it allocates another pointer (internal to the object) to point to the string value. 当您构造一个std::string
对象时,它会分配另一个指针(在对象内部)以指向字符串值。 This is where the third allocation is coming from, and is also the indirectly-leaked memory. 这是第三个分配来自的地方,也是间接泄漏的内存。
In other words, you have these three allocations: 换句话说,您具有以下三个分配:
new Valgrind_testclass()
(explicit) new Valgrind_testclass()
(明确) new std::string("String in heap")
(explicit) new std::string("String in heap")
(显式) Since you leaked allocation 2, you indirectly leaked allocation 3 as well; 由于您泄漏了分配2,因此也间接泄漏了分配3。 the string's destructor will not be called, and therefore it has no opportunity to free allocation 3. 字符串的析构函数将不会被调用,因此它没有机会释放分配3。
You have 3 allocations because std::string
also allocates memory. 您有3个分配,因为std::string
也分配了内存。
Indirectly lost, means that you lost a pointer to something that had a pointer to some other memory. 间接丢失意味着您丢失了指向某个具有其他内存的指针的指针。 In this case, you didn't delete stringInHeap
, and you lost its pointer. 在这种情况下,您没有删除stringInHeap
,并且丢失了它的指针。 Through that, the actual std::string
who had allocated memory could not delete it and therefore that memory is also lost. 这样,分配了内存的实际std::string
无法删除它,因此该内存也丢失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.