samedi 23 mai 2020

Valgrind shows memory leaks with unique pointer. what am i doing wrong?

I've been playing around with smart pointers in C++. I'm curious as to why Valgrind shows memory leaks with this unique_ptr implementation.

#include <iostream>
#include <memory>
#include <vector>

int main()
{
    std::unique_ptr<std::vector<int>> ptr{new std::vector<int>};
    std::cout << ptr.get() << std::endl;

    ptr->emplace_back(1);
    ptr->emplace_back(2);
    ptr->emplace_back(3);

    for (size_t i = 0; i < ptr->size(); i++)
    {
        std::cout << " " << (*ptr)[i];
    }
    std::cout << std::endl;

    return 0;
}

valgrind output

Aucun commentaire:

Enregistrer un commentaire