TextBox-6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 03:58:01 24 4
gpt4 key购买 nike

我试图从 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com