Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C+中的Valgrind错误+;内存泄漏检查_C++_Valgrind_Memcheck - Fatal编程技术网

C++ C+中的Valgrind错误+;内存泄漏检查

C++ C+中的Valgrind错误+;内存泄漏检查,c++,valgrind,memcheck,C++,Valgrind,Memcheck,我试图弄清楚Valgrind的用法,所以我做了一个简单的程序,但当我在课堂上进行动态内存分配时,Valgrind似乎出现了一个奇怪的错误 我的计划是: class Valgrind_testclass { Valgrind_testclass *Obj; public: Valgrind_testclass() { // Obj = new Valgrind_testclass(); // Test point #3 } ~Valgrind_testclass() {

我试图弄清楚Valgrind的用法,所以我做了一个简单的程序,但当我在课堂上进行动态内存分配时,Valgrind似乎出现了一个奇怪的错误

我的计划是:

class Valgrind_testclass
{
 Valgrind_testclass *Obj;
public:
  Valgrind_testclass() { 
    // Obj = new Valgrind_testclass();  // Test point #3
  }
  ~Valgrind_testclass() {
    //delete Obj;               // Test point #4
  }
};

int main()
{
 Valgrind_testclass valObj;
 // Valgrind_testclass * valObjPtr = new Valgrind_testclass();  // Test point #1
 //delete valObjPtr;                 // Test point #2
 return 0;
}
通过注释掉的测试点,Valgrind给了我以下信息:

==5273== HEAP SUMMARY:
==5273==     in use at exit: 0 bytes in 0 blocks
==5273==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
我想这是预期的结果

现在,当我取消注释测试点#1时:

这也是正确的结果,因为我没有执行删除

在测试点#2未注释的情况下,我得到:

==5300== HEAP SUMMARY:
==5300==     in use at exit: 0 bytes in 0 blocks
==5300==   total heap usage: 1 allocs, 1 frees, 4 bytes allocated
==5300== 
==5300== All heap blocks were freed -- no leaks are possible
再次给出正确的结果

现在我取消对测试点#3的注释,希望Valgrind能够检测内存泄漏。这就是我得到的:

==5313== Process terminating with default action of signal 11 (SIGSEGV)
==5313==  Access not within mapped region at address 0xBE17AFE4
==5313==    at 0x40263A0: operator new(unsigned int) (vg_replace_malloc.c:255)
==5313==  If you believe this happened as a result of a stack
==5313==  overflow in your program's main thread (unlikely but
==5313==  possible), you can try to increase the size of the
==5313==  main thread stack using the --main-stacksize= flag.
==5313==  The main thread stack size used in this run was 8388608.
==5313== Stack overflow in thread 1: can't grow stack to 0xbe17afe0
==5313== 
==5313== Process terminating with default action of signal 11 (SIGSEGV)
==5313==  Access not within mapped region at address 0xBE17AFE0
==5313==    at 0x402040C: _vgnU_freeres (vg_preloaded.c:58)
==5313==  If you believe this happened as a result of a stack
==5313==  overflow in your program's main thread (unlikely but
==5313==  possible), you can try to increase the size of the
==5313==  main thread stack using the --main-stacksize= flag.
==5313==  The main thread stack size used in this run was 8388608.
==5313== 
==5313== HEAP SUMMARY:
==5313==     in use at exit: 1,047,576 bytes in 261,894 blocks
==5313==   total heap usage: 261,894 allocs, 0 frees, 1,047,576 bytes allocated
==5313== 
==5313== Searching for pointers to 261,894 not-freed blocks
==5313== Checked 5,339,952 bytes
==5313== 
==5313== LEAK SUMMARY:
==5313==    definitely lost: 0 bytes in 0 blocks
==5313==    indirectly lost: 0 bytes in 0 blocks
==5313==      possibly lost: 0 bytes in 0 blocks
==5313==    still reachable: 1,047,576 bytes in 261,894 blocks
==5313==         suppressed: 0 bytes in 0 blocks
==5313== Reachable blocks (those to which a pointer was found) are not shown.
==5313== To see them, rerun with: --leak-check=full --show-reachable=yes
==5313== 
==5313== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 17 from 6)
我不确定为什么我在这里没有看到类似的内存泄漏错误。 有没有人能帮我了解一下这种情况,我是不是做错了什么。 我使用的Valgrind命令是:

valgrind -v --tool=memcheck --leak-check=full --num-callers=40  ./ValgrindOutput 

谢谢。

节目没有正常结束。您看到的是堆栈溢出错误


构造函数执行调用构造函数的
newvalgrind\u testclass()
。无限递归

这是因为有无限递归


Valgrind\u测试类是一个新的Valgrind\u测试类它是另一个Valgrind\u测试类的新版本

堆栈溢出,而不是堆溢出<代码>线程1中的堆栈溢出:无法将堆栈增长到0xbe17afe0
valgrind -v --tool=memcheck --leak-check=full --num-callers=40  ./ValgrindOutput