删除std ::线程指针引发异常“libc ++ abi.dylib:terminating” [英] Deleting std::thread pointer raises exception "libc++abi.dylib: terminating"
问题描述
在Mac OS X上使用LLVM 6.0的C ++ 11中,我首先创建了一个指向std :: thread的内存分配的指针。
std :: thread * th = new std :: thread([&](int tid){
// do nothing。
},0);
然后我试图删除它。
delete th;但是,编译上面的代码并执行它会引发异常
com / w / cpp / thread / thread /〜thread>析构函数。所以你需要
$ b
libc ++ abi.dylib:terminating
中止陷阱:6
解决方案您创建的主题是
joinable
,除非您加入
或detach
, std :: terminateth-> join();
delete th;
std的早期提案:: thread
implicitly分离
在析构函数中的线程,但是这被发现导致问题时,拥有线程抛出一个异常创建和加入
N2802 包含更改提议及说明性示例。 p>线程
实例的
原来的行为从
boost :: thread
继承,但它也有已淘汰在析构函式中隐藏detach
。
与您的问题无关,但不太可能需要动态分配线程对象,即使您,您应该将它存储在
unique_ptr
中。In C++ 11 with LLVM 6.0 on Mac OS X, I first created a pointer to a memory allocation of std::thread.
std::thread* th = new std::thread([&] (int tid) { // do nothing. }, 0);
Then I tried to delete it.
delete th;
However, compiling the above code and execute it raises exception
libc++abi.dylib: terminating Abort trap: 6
解决方案The thread you've created is
joinable
, and unless youjoin
ordetach
it,std::terminate
will be called when the destructor of the thread object executes. So you needth->join(); delete th;
Early proposals for
std::thread
implicitlydetach
ed the thread in the destructor, but this was found to cause problems when the owning thread threw an exception between creation andjoin
ing of athread
instance. N2802 contains the change proposal along with illustrative examples.The original behavior was carried over from
boost::thread
but it too has since deprecated implicitdetach
in the destructor.
Unrelated to your problem, but it is very unlikely you need to dynamically allocate the thread object, and even if you do, you should be storing it in a
unique_ptr
.这篇关于删除std ::线程指针引发异常“libc ++ abi.dylib:terminating”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!