[英]wxWidgets - terminate called without an active exception (using std::thread)
I am writing GUI application which use my own library which is based on boost::asio
an C++11 standard library. 我正在编写GUI应用程序,它使用我自己的基于C ++ 11标准库
boost::asio
的库。 This is implementation of gui_client::OnInit
and gui_client::OnExit
methods of class gui_client
which derives from wxApp
: 这是从
wxApp
派生的gui_client
类的gui_client::OnInit
和gui_client::OnExit
方法的gui_client
:
bool gui_client::OnInit()
{
io_service_ = new boost::asio::io_service;
client_ = new client(*io_service_);
frame = new main_frame();
std::thread reader([this]() {
// thread code
});
reader_thread = &reader;
frame->Show();
Debug("Returning OnInit");
return true;
}
int gui_client::OnExit()
{
reader_thread->join();
return 0;
}
Everything compiles and application starts. 一切都会编译并开始应用程序。 I see debug information in command line ('Returning OnInit') and then:
我在命令行中看到调试信息(“ Returning OnInit”),然后:
terminate called without an active exception
在没有活动异常的情况下终止调用
I tried to set breakpoints in gdb
on some wxApp
functions, but I cannot find the place where error is returned. 我试图在某些
wxApp
函数的gdb
设置断点,但是找不到返回错误的位置。 gui_client
class has only three methods: gui_client
类只有三种方法:
virtual bool OnInit()
virtual int OnExit()
client * get_client()
When I do not start reader thread ( std::thread reader
) everything works well. 当我不启动读取器线程(
std::thread reader
)时,一切工作正常。 When I started thread with empty loop in lambda function I got error which I mentioned above. 当我在lambda函数中使用空循环启动线程时,出现了上面提到的错误。 I'm also sure that thread code is correct because the same code works well in CLI testing application.
我还确定线程代码正确,因为相同的代码在CLI测试应用程序中效果很好。
I use wxWidgets 3.0.1.0
and g++ 4.7.2
(Debian). 我使用
wxWidgets 3.0.1.0
和g++ 4.7.2
(Debian)。 I use this g++ command for comilation: 我使用以下g ++命令进行编译:
g++ `wx-config --version=3.0 --cxxflags` -std=c++11 -I./headers -I../headers -L../lib/Debug -DDEBUG -g -c -o [obj_file] [source]
and this command for linking: 和此命令链接:
g++ `wx-config --version=3.0 --cxxflags` -std=c++11 -I./headers -I../headers -L../lib/Debug -DDEBUG -g [obj_files] -ltamandua `wx-config --version=3.0 --libs` -lboost_system -pthread -o [output]
Problem is here: 问题在这里:
std::thread reader([this]() {
// thread code
});
reader_thread = &reader;
reader
will be destroyed after OnInit
function ends (and terminate will be called, since thread
is joinable). 在
OnInit
函数结束后, reader
将被销毁(并且将终止,因为thread
是可连接的,所以将被调用)。 You should use smart-pointer in class in this case, or create reader_thread
using new
, or simply save thread in object and assign it to your reader_thread
(reader_thread should be object, not pointer) via move. 在这种情况下,您应该在类中使用智能指针,或者使用
new
创建reader_thread
,或者只是简单地将线程保存在对象中,然后通过移动将其分配给您的reader_thread
(reader_thread应该是对象,而不是指针)。
1) reader_thread = std::make_shared<std::thread>([this]() {});
1)
reader_thread = std::make_shared<std::thread>([this]() {});
2) reader_thread = new std::thread([this]() {});
2)
reader_thread = new std::thread([this]() {});
3) 3)
std::thread reader([this](){});
reader_thread = std::move(reader);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.