意外内存泄漏 [Valgrind]

发布于 2024-08-19 14:35:02 字数 2369 浏览 5 评论 0原文

今天我只是想检查一下 valgrind 是如何工作的。所以我创建了一个简单的程序。

//leak.C
#include<iostream>

class leak
{

   int *p;

   public:

   leak():p(new int[10]()){std::cout<<"Constructor of leak called\n";}
   virtual void set()
   {

       for(int i=0;i<10;++i) p[i]=i*i;
   }

   virtual void display()
   {
       std::cout<<"In leak's display()\n";
       for(int i=0;i<10;++i) std::cout<<p[i]<<std::endl;
   }

   virtual ~leak()
   {
       std::cout<<"Destructor of leak called\n";
       delete[] p;
   }
};

class exleak: public leak
{

     double *r;

     public:

     exleak():r(new double[5]()){std::cout<<"Constructor of exleak called\n";}

     void set()
     {
       leak::set();
       for(int i=0;i<5;i++) r[i]=i*3.0;
     }


     void display()
     {
         leak::display();
         std::cout<<"In exleak's display()\n";
         for(int i=0;i<5;i++)  std::cout<<r[i]<<std::endl;
     }

     ~exleak()
     {

          std::cout<<"Destructor of exleak called\n";
          delete[] r;
     }
};

int main()
{

     leak *x=new exleak();
     x->set();
     x->display();
     delete x;

}

输出符合预期。我预计不会出现内存泄漏。 我编译了文件 leak.C 并生成了可执行文件 leak。 但当我输入以下命令 valgrind --leak-check=yes --verbose ./leak 时,我感到惊讶。该代码存在内存泄漏。 :-o

这就是我得到的。

==9320== 
==9320== HEAP SUMMARY:
==9320==     in use at exit: 12 bytes in 1 blocks
==9320==   total heap usage: 3 allocs, 2 frees, 92 bytes allocated
==9320== 
==9320== 12 bytes in 1 blocks are definitely lost in loss record 1 of 1
==9320==    at 0x40263A0: operator new(unsigned int) (vg_replace_malloc.c:214)
==9320==    by 0x8048B0E: main (in /home/prasoon/leak)
==9320== 
==9320== LEAK SUMMARY:
==9320==    definitely lost: 12 bytes in 1 blocks
==9320==    indirectly lost: 0 bytes in 0 blocks
==9320==      possibly lost: 0 bytes in 0 blocks
==9320==    still reachable: 0 bytes in 0 blocks
==9320==         suppressed: 0 bytes in 0 blocks
==9320== 
==9320== For counts of detected and suppressed errors, rerun with: -v
==9320== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 25 from 6)

代码如何泄漏内存?

  definitely lost: 12 bytes in 1 blocks //WHERE?

编辑问题已解决

Today I was just trying to check how valgrind works. So I created a simple program.

//leak.C
#include<iostream>

class leak
{

   int *p;

   public:

   leak():p(new int[10]()){std::cout<<"Constructor of leak called\n";}
   virtual void set()
   {

       for(int i=0;i<10;++i) p[i]=i*i;
   }

   virtual void display()
   {
       std::cout<<"In leak's display()\n";
       for(int i=0;i<10;++i) std::cout<<p[i]<<std::endl;
   }

   virtual ~leak()
   {
       std::cout<<"Destructor of leak called\n";
       delete[] p;
   }
};

class exleak: public leak
{

     double *r;

     public:

     exleak():r(new double[5]()){std::cout<<"Constructor of exleak called\n";}

     void set()
     {
       leak::set();
       for(int i=0;i<5;i++) r[i]=i*3.0;
     }


     void display()
     {
         leak::display();
         std::cout<<"In exleak's display()\n";
         for(int i=0;i<5;i++)  std::cout<<r[i]<<std::endl;
     }

     ~exleak()
     {

          std::cout<<"Destructor of exleak called\n";
          delete[] r;
     }
};

int main()
{

     leak *x=new exleak();
     x->set();
     x->display();
     delete x;

}

The output was as expected. I expected no memory leak.
I compiled the file leak.C and generated an executable leak.
But when I entered the following command valgrind --leak-check=yes --verbose ./leak, I was surprised. The code had a memory leak. :-o

This is what I got.

==9320== 
==9320== HEAP SUMMARY:
==9320==     in use at exit: 12 bytes in 1 blocks
==9320==   total heap usage: 3 allocs, 2 frees, 92 bytes allocated
==9320== 
==9320== 12 bytes in 1 blocks are definitely lost in loss record 1 of 1
==9320==    at 0x40263A0: operator new(unsigned int) (vg_replace_malloc.c:214)
==9320==    by 0x8048B0E: main (in /home/prasoon/leak)
==9320== 
==9320== LEAK SUMMARY:
==9320==    definitely lost: 12 bytes in 1 blocks
==9320==    indirectly lost: 0 bytes in 0 blocks
==9320==      possibly lost: 0 bytes in 0 blocks
==9320==    still reachable: 0 bytes in 0 blocks
==9320==         suppressed: 0 bytes in 0 blocks
==9320== 
==9320== For counts of detected and suppressed errors, rerun with: -v
==9320== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 25 from 6)

How is the code leaking memory?

  definitely lost: 12 bytes in 1 blocks //WHERE?

EDIT : Matter Resolved.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉生梦死 2024-08-26 14:35:02

我在使用 2.6.18-164.11.1.el5 内核和 GCC 4.1.2 的 Linux 上尝试过,它没有给我任何泄漏

,而且我没有看到代码有任何问题

I tried it on Linux with 2.6.18-164.11.1.el5 kernel and GCC 4.1.2, it does not give me any leak

and i don't see any issue with the code

何以笙箫默 2024-08-26 14:35:02

gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
valgrind-3.11.0

没有收到任何警告

在此处输入图像描述

gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
valgrind-3.11.0

do not get any warning

enter image description here

摇划花蜜的午后 2024-08-26 14:35:02

类 exleak 没有虚拟析构函数...

class exleak has no virtual destructor...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文