dimanche 31 mai 2015

Taking value from unique_ptr without generating memory leaks

I'm allocating several unique_ptr<int>s and I then pass these into an insert function that then calls an overloaded insert function and passes it the value derived from std::move(uniquePtr).

The overloaded insert does what it is supposed to do, but then I have a ton of memory leaks.

How can I eliminate these memory leaks?

typedef std::unique_ptr<int> UP;
std::vector<int> data{ 9, 10, 7, 8, 5, 6, 3, 4, 1, 2 };
custom_container<UP> s;
for (auto datum : data) {
    s.insert(UP(new int(datum)));
}

template <typename U>
void insert(std::unique_ptr<U> ptr)
{
    this->insert(std::move(ptr));
}

Aucun commentaire:

Enregistrer un commentaire