0

1 std::unique_ptr< Test[] > upt(std::make_unique< Test[] >(2));

//No leak here, as it does reset, which will deallocate.

3 upt = (std::make_unique< Test[] >(1));

In the above snippet, on call to line 3, twice destructors of Test gets called, and allocates memory for array of size 1,

So, who call the reset? Is it make_unique does it? Also, can I get detail implementation of make_unique and make_shared.

kit
  • 1,166
  • 5
  • 16
  • 23
  • It can't be `make_unique`. It has to be `operator=`. – David Schwartz Nov 15 '18 at 04:00
  • class Test { private: int m_a; public: Test(int i) :m_a(i) {} Test() :m_a(0) {} ~Test() { std::cout << "Test: dtor\n"; } int geta() { return m_a; } void seta(int a) { m_a = a; } }; – Reshma Dhotre Nov 15 '18 at 04:00
  • For future readers: https://stackoverflow.com/questions/37806616/reassign-unique-ptr-object-with-make-unique-statements-memory-leak/37806844 – dvlper May 25 '20 at 09:08

0 Answers0