Posts

Showing posts with the label unique-ptr

valgrind shows memory leak in std::make_unique

I'm using valgrind to check for memory leaks. Unfortunately I get a Leak_DefinitelyLost warning. Attached is a simplified version of my code that reproduces the error: #include <iostream> #include <vector> #include <memory> #include <unordered_map> using namespace std; class Base{ public: explicit Base(double a){ a_ = a; } virtual void fun() = 0; protected: double a_; }; class Derived_A : public Base{ public: Derived_A(double a, vector<double> b, vector<double> c): Base(a), b_{b}, c_{c}{ } void fun() override{ cout << "Derived_A " << a_ << endl; } private: vector<double> b_; vector<double> c_; }; class Derived_B : public Base{ public: Derived_B(double a, double b, double c): Base(a), b_{b}, c_{c}{ } void fun() override{ cout << "Derived_B " << a_ << endl; } private: double b_; double c_; }; in...

Pimpl - Why can make_unique be called on an incomplete type

Why does the make_unique call compile? Doesn't make_unqiue require its template argument to be a complete type ? struct F; int main() { std::make_unique<F>(); } struct F {}; The question orignated from my "problem" with my PIMPL implementation: I do understand why the destructor has to be user declared and defined inside the cpp file for the Implementation class (PIMPL). But moving the constructor of the class containing the pimpl- still compiles. class Object {}; class CachedObjectFactory { public: CachedObjectFactory(); ~CachedObjectFactory(); std::shared_ptr<Object> create(int id) const; private: struct CacheImpl; std::unique_ptr<CacheImpl> pImpl; }; Now the cpp file: // constructor with make_unique on incompete type ? CachedObjectFactory::CachedObjectFactory() : pImpl(std::make_unique<CacheImpl>()) {} struct CachedObjectFactory::CacheImpl { std::map<int, std::shared_ptr<Object>> idToObjects; }; //deferred destructor ...