c++ - 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
解决方案
推荐阅读
- apache-spark - Spark 2.2 使用旧的 com.google.guava 库版本:14 但我需要在 SBT 项目中使用 guava 版本 18 及更高版本
- python - 用 tweepy 发布带有各自图像的不同推文
- c# - 采购失败,因为采购未正确初始化
- hibernate - 如何在序列化期间限制休眠相关对象
- c++ - 将变量传递给函数\使用该变量返回一个新值\重复直到值达到 0
- scala - 如何仍然在 Scala 的单例对象中执行超类的代码体
- c# - 如何创建具有多个查询的实体
- ruby-on-rails - has_many: : 是时候通过控制器了吗?如果是这样,那是什么样的?
- javascript - Javascript - 我应该如何编写一个可重用的方法来等待一组事件中的一个发生?
- html - React 中的图像渲染与 webpack 损坏