c++ - 将 boost::thread 与 wxWidgets GUI 混合时出现 "Unhandled exception"错误

标签 c++ boost multithreading wxwidgets

我试图从 boost::thread 访问 wxDialog 成员:

void AnotherThread(myWxDialog *dlg)
{
  wxMessageBox(dlg->TextBox1->GetValue(), "It works!"); // This throws an error
}

void myWxDialog::OnButtonClick(wxCommandEvent &event)
{
  boost::thread myThread(AnotherThread, this);
}

我得到了这个错误:

MyProgram.exe 中 0x004043d7 处的未处理异常:0xC0000005:访问冲突读取位置 0xbaadf00d。

我认为这种行为在不同线程之间是不允许的。

那么,有没有其他方法可以做同样的事情呢?

如有任何帮助,我们将不胜感激。 :)

(微软 Visual C++ 2008 速成版)

最佳答案

0xbaadf00d 表示您正在取消引用未初始化的指针;如果我是你,我会更深入地使用调试器来查看指针的确切位置(在 dlg 中?在 TextBox1 中?在 GetValue() 返回的内容中?在 wxMessageBox 中的其他地方?)。这将帮助您了解问题出在哪里。

仍然,最大的错误是试图从另一个线程访问 GUI 事物:如明确说明 here ,

When writing a multi-threaded application, it is strongly recommended that no secondary threads call GUI functions. The design which uses one GUI thread and several worker threads which communicate with the main one using events is much more robust and will undoubtedly save you countless problems (example: under Win32 a thread can only access GDI objects such as pens, brushes, device contexts created by itself and not by the other threads).

在那里您还可以找到一些关于如何使用事件和其他 wxWidgets 设施解决这些限制的建议。

关于c++ - 将 boost::thread 与 wxWidgets GUI 混合时出现 "Unhandled exception"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2601290/

相关文章:

C++:在方法内部可以从类创建未初始化的对象吗?

c++ - 为什么我的代码返回错误的平方值? C++

c++ - 使用boost asio从串口读取最新数据

python - 使用多线程优化 python 脚本

java - 程序允许重复设置。怎么可能?

c++ - 以 `DialogBox` 之外的其他方法打开窗口

c++ - boost 单元解析字符串流

boost - 在 CentOS : can't find Boost 1. 40 或更高版本上安装 Thrift

c++ - 使用 pthreads 作为类成员的互斥体

c++ - OpenCV - 禁用打印异常