70024

thread_local std::unique_ptr release not calling destructor

Question:

Why isn't the destructor called in <a href="http://coliru.stacked-crooked.com/a/aacad1384aa6eda8" rel="nofollow">this code</a>:

#include <iostream> #include <thread> #include <memory> class base { public: base() { std::cout << "base()" << std::endl; } virtual ~base() { std::cout << "~base()" << std::endl; } base(const base&) = delete; base(base&&) = delete; base& operator=(const base&) = delete; base& operator=(base&&) = delete; }; class derived final : public base { public: derived() { std::cout << "derived()" << std::endl; } virtual ~derived() { std::cout << "~derived()" << std::endl; } }; void foo() { static thread_local std::unique_ptr<base> ptr; if (!ptr) { std::cout << "new ptr:" << std::this_thread::get_id() << std::endl; ptr.reset(new derived()); } else { std::cout << "release ptr:" << std::this_thread::get_id() << std::endl; ptr.release(); // I would expect the destructor to be called here?! } } void thread_main() { foo(); foo(); } int main() { std::thread thread1(thread_main); thread1.join(); return 0; }

Output:

new ptr:140671459997440 base() derived() release ptr:140671459997440

I would expect:

new ptr:140671459997440 base() derived() release ptr:140671459997440 ~derived() ~base()

Using gcc 4.9.1

Answer1:

Replace ptr.release(); with ptr.reset();.

Recommend

  • What is the Performance, Safety, and Alignment of a Data member hidden in an embedded char array in
  • Original method still getting called in Moq even after CallBase = true/false
  • Renaming class causes IBOutlet connection to fail
  • How do i do the following curl command in Java
  • Is it one-to-one or Component ? Nhibernate Mapping
  • Need help to stop program terminating without users consent
  • Installing PAR::Packer on Windows, dmake error 255
  • Iterating over a container bidirectionally
  • What's the essential difference between these two variadic functions?
  • std::system Exception when instantiating a singleton object
  • OpenMP and File I/O
  • Specifying virtual keyboard type for EditText in XML
  • runtime-check whether an instance (Base*) override a parent function (Base::f())
  • c++ regex_replace not doing intended substitution
  • Building Qt project for C++11 standard
  • C++ friend class std::vector
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • RxJava debounce by arbitrary value
  • Reduction and collapse clauses in OMP have some confusing points
  • C: Incompatible pointer type initializing
  • why overloaded new operator is calling constructor even I am using malloc inside overloading functio
  • Adding a button at the bottom of a table view
  • Getting last autonumber in access
  • javaw.exe and eclipse startup problems
  • AES padding and writing the ciphertext to a disk file
  • VS2008 Enable C++ Exception with SEH
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • How to model a transition system with SPIN
  • Delete MySQLi record without showing the id in the URL
  • Rearranging Cells in UITableView Bug & Saving Changes
  • GridView Sorting works once only
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • Python: how to group similar lists together in a list of lists?
  • JTable with a ScrollPane misbehaving
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • unknown Exception android
  • Django query for large number of relationships
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?