void DeleteNodes(BTNode* node)
{
// Recurse left down the tree...
if(node->left != NULL)
{
DeleteNodes(node->left);
}
if(node->right != NULL)
{
DeleteNodes(node->right);
}
// Free memory used by the node itself.
delete node;
}
Valgrind对我大吼大叫。
==20793== Invalid read of size 8
==20793== at 0x407B68: toString(std::string, NodeInterface*) (in /users/guest/j/jnuss2/Desktop/To Students BST/dont_run_me)
==20793== by 0x4081CF: printBST(BSTInterface*) (in /users/guest/j/jnuss2/Desktop/To Students BST/dont_run_me)
==20793== by 0x40856F: printError() (in /users/guest/j/jnuss2/Desktop/To Students BST/dont_run_me)
==20793== by 0x4028E9: main (in /users/guest/j/jnuss2/Desktop/To Students BST/dont_run_me)
==20793== Address 0x4c448b0 is 0 bytes inside a block of size 32 free'd
==20793== at 0x4A07991: operator delete(void*) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==20793== by 0x407785: BTNode::~BTNode() (in /users/guest/j/jnuss2/Desktop/To Students BST/dont_run_me)
==20793== by 0x407213: DeleteChildren(BTNode*) (in /users/guest/j/jnuss2/Desktop/To Students BST/dont_run_me)
==20793== by 0x4075A7: BST::clear() (in /users/guest/j/jnuss2/Desktop/To Students BST/dont_run_me)
==20793== by 0x402874: main (in /users/guest/j/jnuss2/Desktop/To Students BST/dont_run_me)
==20793==
pure virtual method called
terminate called without an active exception
我一直在调试这段特殊的代码。在我实现这篇文章之前,该程序没有报告这些内存泄漏。我查找了不同的Valgrind错误,我理解大小为8的无效读取意味着它正在尝试访问已经释放的内存(http://valgrind.org/docs/manual/mc-manual.html)。但是,我无法弄清楚递归算法一遍又一遍地访问同一个内存的位置。我还在StackExchange上找到了一些其他答案,如:Valgrind... 4 bytes inside a block of size 8 free'd。 它对我的情况没有帮助,因为我没有使用双指向我的知识。无论如何,如果需要更多的代码,我可以发布,但这是迄今为止唯一提出问题的代码。我也可以在这个.cpp文件中发布剩下的代码,但据我所知,它无关紧要,因为在我输入此代码之前没有内存泄漏错误。
这是Valgrind完成运行后的内存报告。
==20793== HEAP SUMMARY:
==20793== in use at exit: 8,953 bytes in 6 blocks
==20793== total heap usage: 2,036 allocs, 2,030 frees, 811,313 bytes allocated
==20793==
==20793== 24 bytes in 1 blocks are still reachable in loss record 1 of 6
==20793== at 0x4A06965: operator new(unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==20793== by 0x401DC3: main (in /users/guest/j/jnuss2/Desktop/To Students BST/dont_run_me)
==20793==
==20793== 26 bytes in 1 blocks are possibly lost in loss record 2 of 6
==20793== at 0x4A06965: operator new(unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==20793== by 0x30020BDCF8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /usr/lib64/libstdc++.so.6.0.19)
==20793== by 0x30020BF5B0: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (in /usr/lib64/libstdc++.so.6.0.19)
==20793== by 0x30020BF9E7: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib64/libstdc++.so.6.0.19)
==20793== by 0x40819C: printBST(BSTInterface*) (in /users/guest/j/jnuss2/Desktop/To Students BST/dont_run_me)
==20793== by 0x40856F: printError() (in /users/guest/j/jnuss2/Desktop/To Students BST/dont_run_me)
==20793== by 0x4028E9: main (in /users/guest/j/jnuss2/Desktop/To Students BST/dont_run_me)
==20793==
==20793== 32 bytes in 1 blocks are still reachable in loss record 3 of 6
==20793== at 0x4A06965: operator new(unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==20793== by 0x407838: Factory::getBST() (in /users/guest/j/jnuss2/Desktop/To Students BST/dont_run_me)
==20793== by 0x401DDA: main (in /users/guest/j/jnuss2/Desktop/To Students BST/dont_run_me)
==20793==
==20793== 111 bytes in 1 blocks are possibly lost in loss record 4 of 6
==20793== at 0x4A06965: operator new(unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==20793== by 0x30020BDCF8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /usr/lib64/libstdc++.so.6.0.19)
==20793== by 0x30020BE93A: std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned long) (in /usr/lib64/libstdc++.so.6.0.19)
==20793== by 0x30020BE9E3: std::string::reserve(unsigned long) (in /usr/lib64/libstdc++.so.6.0.19)
==20793== by 0x30020BEAA7: std::string::append(std::string const&) (in /usr/lib64/libstdc++.so.6.0.19)
==20793== by 0x408A16: printBeforeError() (in /users/guest/j/jnuss2/Desktop/To Students BST/dont_run_me)
==20793== by 0x4026F3: main (in /users/guest/j/jnuss2/Desktop/To Students BST/dont_run_me)
==20793==
==20793== 568 bytes in 1 blocks are still reachable in loss record 5 of 6
==20793== at 0x4A0645D: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==20793== by 0x3FFD46C52C: __fopen_internal (in /usr/lib64/libc-2.18.so)
==20793== by 0x300207CA1F: std::__basic_file<char>::open(char const*, std::_Ios_Openmode, int) (in /usr/lib64/libstdc++.so.6.0.19)
==20793== by 0x30020B77F9: std::basic_filebuf<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode) (in /usr/lib64/libstdc++.so.6.0.19)
==20793== by 0x40853A: printError() (in /users/guest/j/jnuss2/Desktop/To Students BST/dont_run_me)
==20793== by 0x4028E9: main (in /users/guest/j/jnuss2/Desktop/To Students BST/dont_run_me)
==20793==
==20793== 8,192 bytes in 1 blocks are still reachable in loss record 6 of 6
==20793== at 0x4A0700A: operator new[](unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==20793== by 0x30020B71CB: std::basic_filebuf<char, std::char_traits<char> >::_M_allocate_internal_buffer() (in /usr/lib64/libstdc++.so.6.0.19)
==20793== by 0x30020B7811: std::basic_filebuf<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode) (in /usr/lib64/libstdc++.so.6.0.19)
==20793== by 0x40853A: printError() (in /users/guest/j/jnuss2/Desktop/To Students BST/dont_run_me)
==20793== by 0x4028E9: main (in /users/guest/j/jnuss2/Desktop/To Students BST/dont_run_me)
==20793==
==20793== LEAK SUMMARY:
==20793== definitely lost: 0 bytes in 0 blocks
==20793== indirectly lost: 0 bytes in 0 blocks
==20793== possibly lost: 137 bytes in 2 blocks
==20793== still reachable: 8,816 bytes in 4 blocks
==20793== suppressed: 0 bytes in 0 blocks