thread_local std :: unique_ptr释放不调用析构函数

Mat*_*son 0 c++ smart-pointers thread-local-storage c++11

为什么不在此代码中调用析构函数:

#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;
}
Run Code Online (Sandbox Code Playgroud)

输出:

new ptr:140671459997440
base()
derived()
release ptr:140671459997440
Run Code Online (Sandbox Code Playgroud)

我希望:

new ptr:140671459997440
base()
derived()
release ptr:140671459997440
~derived()
~base()
Run Code Online (Sandbox Code Playgroud)

运用 gcc 4.9.1

Ker*_* SB 7

替换ptr.release();为ptr.reset();.