简体   繁体   中英

vector pointer causing valgrind memory leak

I'm trying to learn C++ and valgrind. So I wrote the following code to test it out. But I get some memory leaks. Can anyone explain what's causing the memory leak? Thanks in advance.

#include <vector>
#include <iostream>
using namespace std;

class test
{
     int c;
      public:
      void whatever();
}; 
void test:: whatever()
{
     vector<test*> a;
     if(true)
     {
           test* b = new test();
           b->c = 1;
           a.push_back(b);
     }
     test* d = a.back();
     cout << "prints: " << d->c;
     delete d;
}

int main()
{
    test* a = new test();
    a->whatever();
    return 1;
}

from valgrind

==28548== HEAP SUMMARY:
==28548==     in use at exit: 4 bytes in 1 blocks
==28548==   total heap usage: 3 allocs, 2 frees, 16 bytes allocated
==28548==
==28548== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==28548==    at 0x4C27CC1: operator new(unsigned long) (vg_replace_malloc.c:261)
==28548==    by 0x400C36: main (in a.out)
==28548==
==28548== LEAK SUMMARY:
==28548==    definitely lost: 4 bytes in 1 blocks
==28548==    indirectly lost: 0 bytes in 0 blocks
==28548==      possibly lost: 0 bytes in 0 blocks
==28548==    still reachable: 0 bytes in 0 blocks
==28548==         suppressed: 0 bytes in 0 blocks
==28548==
==28548== For counts of detected and suppressed errors, rerun with: -v
==28548== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)

Am I not allowed to delete from a copy of a pointer or am I doing something else wrong?

You never call delete on a .

Of course, the important bit here is that you are using a vector of pointers. Why in the world would you do this? Let the vector take care of memory management for you!

You forgot to delete a; at the end of main() .

Note that everything you wrote should never go into real code. You should never use dynamic allocation ( new ) unless you absolutely have to and know precisely why.


Assuming you want to maintain the vector of pointers for educational purposes, then here's a better way of writing it:

#include <vector>
#include <memory>  // for unique_ptr

// intentionally left blank; NO abusing namespace std!

struct Foo
{
    int c;

    void whatever()
    {
        std::vector<std::unique_ptr<test>> v;

        if (true)
        {
            v.emplace_back(new test);
            v.back()->c = 1;
        }

        // everything is cleaned up automagically
    }
};

int main()
{
    Test a;        // automatic, not dynamic
    a.whatever();

    return 1;
}

This is still just for educational purposes; in real life you would try very hard to make do with a plain std::vector<test> , since vector is already a dynamic data structure and there is little need for the extra level of indirection.

The memory leak is in main. You are not deleting the allocated test object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM