首页 > 解决方案 > Valgrind 即使在删除操作后也显示泄漏

问题描述

我有一个非常简单的程序如下:

#include<iostream>
using namespace std;

int main()
{
        char *s=new char[10];
        cin>>s;
        cout<<s;
        delete[] s;
}

我无法弄清楚我能做些什么让 valgrind 不抱怨它:

==5139== LEAK SUMMARY:
==5139==    definitely lost: 32 bytes in 1 blocks
==5139==    indirectly lost: 24 bytes in 1 blocks
==5139==      possibly lost: 0 bytes in 0 blocks
==5139==    still reachable: 31,451 bytes in 759 blocks

编辑 2:添加 Valgrind 错误/堆摘要报告:

==42891== LEAK SUMMARY:
==42891==    definitely lost: 20 bytes in 1 blocks
==42891==    indirectly lost: 0 bytes in 0 blocks
==42891==      possibly lost: 0 bytes in 0 blocks
==42891==    still reachable: 528,598 bytes in 11,121 blocks
==42891==         suppressed: 0 bytes in 0 blocks
==42891== Rerun with --leak-check=full to see details of leaked memory
==42891==
==42891== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==42891== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
--42890-- REDIR: 0x52ead60 (libc.so.6:__GI_stpcpy) redirected to 0x4c2d8d0 (__GI_stpcpy)
--42890-- REDIR: 0x52e7590 (libc.so.6:__GI_strcpy) redirected to 0x4c2ab90 (__GI_strcpy)
--42890-- REDIR: 0x52e1450 (libc.so.6:realloc) redirected to 0x4c29aae (realloc)
--42890-- REDIR: 0x52eaf20 (libc.so.6:__GI___strcasecmp_l) redirected to 0x4c2b710 (__GI___strcasecmp_l)
test4.cpp: line 4: syntax error near unexpected token `('
test4.cpp: line 4: `int main()'
==42890==
==42890== HEAP SUMMARY:
==42890==     in use at exit: 528,567 bytes in 11,123 blocks
==42890==   total heap usage: 11,374 allocs, 251 frees, 570,897 bytes allocated
==42890==
==42890== Searching for pointers to 11,123 not-freed blocks
==42890== Checked 768,632 bytes
==42890==
==42890== LEAK SUMMARY:
==42890==    definitely lost: 32 bytes in 1 blocks
==42890==    indirectly lost: 24 bytes in 1 blocks
==42890==      possibly lost: 0 bytes in 0 blocks
==42890==    still reachable: 528,511 bytes in 11,121 blocks
==42890==         suppressed: 0 bytes in 0 blocks
==42890== Rerun with --leak-check=full to see details of leaked memory
==42890==
==42890== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==42890== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

g++ (GCC) 4.8.5 20150623 (红帽 4.8.5-11)

valgrind --version
valgrind-3.11.0

标签: c++valgrind

解决方案


推荐阅读