jeudi 1 février 2018

deleting vector of empty unique_ptr

I've encountered a problem with memory leak when I try to make a vector of unique_ptr. I've tried to find where is the problem and I finally end up with this (meaningless) piece of code (my code contains meaningful operations, here are integers for simplicity):

class Deleter
{
public:

    void operator()(int* ptr)
    {
        delete ptr;
    }
};

and

vector<unique_ptr<int, Deleter>> data;

for (int i = 0; i < 10000; i++)
{
    int* temp = NULL;
    unique_ptr<int, Deleter> uptr;
    uptr = unique_ptr<int, Deleter>(temp);

    data.push_back(move(uptr));
    uptr.~unique_ptr();
}

data.~vector();

This code makes a small memory leak, but I am curious where is the problem, thanks

Aucun commentaire:

Enregistrer un commentaire