SIGABRT 和线程相关的异常,但在调试期间工作正常
SIGABRT and thread related exception but works fine during debug
下面的代码在运行时会崩溃,但在调试期间工作正常。我不明白为什么。我正在根据需要在析构函数中加入线程。
有人可以帮忙吗?
令人惊讶的是,代码在调试期间工作正常。
在调试中尝试了几次,每次似乎都很好。
#include<iostream>
#include<string>
#include<thread>
#include<memory>
using namespace std;
template<typename T> class Runnable
{
private:
std::thread t;
void runImpl()
{
static_cast<T*>(this)->run();
}
public:
virtual void run() = 0;
void start()
{
this->t = std::thread(&Runnable::runImpl, this);
}
~Runnable()
{
std::cout << "I am in the destructor of the base class" << std::endl;
if(t.joinable())
{
std::cout << "joinable" << std::endl;
t.join();
}
else
std::cout << "not joinable" << std::endl;
}
};
class HelloWorld : public Runnable<HelloWorld>
{
public:
void run()
{
std::cout << "I am in thread id = " << std::this_thread::get_id() << " and the message is : HelloWorld How are you" << std::endl;
}
};
int main(int argc, char* argv[])
{
std::cout << "I am in the main thread id = " << std::this_thread::get_id() << std::endl;
{
HelloWorld item;
item.start();
}
return(0);
}
你在*this
上有一个竞争,在调用虚拟成员函数的工作线程和进入析构函数的主线程之间。这表现出未定义的行为。
实际上,vtable 指针可能已经更新为Runnable
的指针,线程最终调用了一个纯虚函数。
相关文章:
- Visual Studio 调试优化如何工作?
- 添加新行时工作代码引发异常.调试技巧?
- 自定义内存管理器在发布模式下工作正常,但在调试模式下则不然
- SIGABRT 和线程相关的异常,但在调试期间工作正常
- 在调试配置中编译工作正常,但发布会给出链接错误
- 从Excel VBA调用C++DLL只能从Visual Studio中的调试实例工作
- Mongodb Cxx 驱动程序测试在发布版本中崩溃 [ 在调试中工作正常]
- 函数无法正常工作,调试器没有帮助
- Android Studio 调试原生代码 lldb 服务器工作无穷大
- 我的代码在发布模式下不起作用,而在Qt的调试模式下工作
- Qt curl.exe在调试/发布中工作,但不在部署中工作
- ifstream读取二进制文件在发行模式下工作,但在调试模式下不使用
- 为什么在发布模式下无法访问 for 循环,但在调试中它工作正常
- CLION 调试器在执行一个步骤后停止工作
- Python在调试模式下以C 工作,但在EXE文件中不工作
- 该代码在调试模式下工作,但在发布模式C 中不起作用
- 在发行模式下开放的软崩溃(调试工作正常)
- 台阶调试工作正常,但是在使用_tprintf时,调试而无需踩踏会导致奇怪的错误
- VS 2012 更改默认调试"工作目录"
- Visual Studio.如何构建DLL(在命令行中),通过附加到进程来调试工作