wxThreadEvent :: GetPayload()

时间:2016-11-15 13:18:25

标签: c++ multithreading wxwidgets

我在Code :: Block中有一个带有gcc和wxWidgets的C ++程序。 在我的工作线程抛出一个带有结构作为有效负载的wxThreadEvent之后,我的程序崩溃了(实际上不是在抛出,但是此刻我想在主体中获得有效负载)。

有没有人知道出了什么问题?

工作线程部分:

wxThread::ExitCode NavigationThread::Entry()
{
  wxThreadEvent event(wxEVT_THREAD, ID_REFRESH_DIRECTION);
  position_variables positionPayload;
  positionPayload.latitude = latDouble;
  positionPayload.longitude = lonDouble; 
  positionPayload.direction = direction;
  event.SetPayload(&positionPayload);
  m_parent->GetEventHandler()->AddPendingEvent(event);
}

结构:

struct position_variables{

    double latitude;
    double longitude;
    wxString direction;
};

class NavigationThread : public wxThread
{
  ...
}

main.cpp

WindowsDgpsGUIFrame::WindowsDgpsGUIFrame(wxWindow* parent,wxWindowID id)
{
     Bind(wxEVT_THREAD, &WindowsDgpsGUIFrame::onRefreshDirections, this, ID_REFRESH_DIRECTION);
}

void WindowsDgpsGUIFrame::onRefreshDirections(wxThreadEvent& event)
{
    position_variables answerDirections = event.GetPayload<position_variables>(); //Here it crashes
}

当在正常的“运行”模式下发生崩溃时,会打开一个窗口,说明程序停止工作。在调试模式下,Code :: blocks中有一个小窗口,说明SIGSEGV, segmentation fault(或类似的东西),这就是调用堆栈:

#0 00877A54 std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::basic_string(std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&) () (??:??)
#1 04A1E550 ?? () (??:??)
#2 007ED139 position_variables::position_variables(this=0x4a1e588) (D:/WindowsDgps/WindowsDgpsGUI/NavigationThread.h:54)
#3 00851B54 wxAny::As<position_variables>(this=0x4c6fe70) (C:/wxWidgets-3.0.2/include/wx/any.h:979)
#4 0084E70C wxEventAnyPayloadMixin::GetPayload<position_variables>(this=0x4c6fe58) (C:/wxWidgets-3.0.2/include/wx/event.h:1219)
#5 0043320E WindowsDgpsGUIFrame::onRefreshDirections(this=0x4be2a68, event=...) (D:\WindowsDgps\WindowsDgpsGUI\WindowsDgpsGUIMain.cpp:440)
#6 0063AA48 wxAppConsoleBase::HandleEvent (this=0x4b2bde0, handler=0x4be2a68, func=(void (wxEvtHandler::*)(wxEvtHandler * const, wxEvent &) (../../src/common/appbase.cpp:611)
#7 0063AAD9 wxAppConsoleBase::CallEventHandler(this=0x4b2bde0, handler=0x4be2a68, functor=..., event=...) (../../src/common/appbase.cpp:623)
#8 0062DEA1 wxEvtHandler::ProcessEventIfMatchesId(entry=..., handler=0x4be2a68, event=...) (../../src/common/event.cpp:1392)
#9 0062EB3A wxEvtHandler::SearchDynamicEventTable(this=0x4be2a68, event=...) (../../src/common/event.cpp:1751)
#10 0062E318    wxEvtHandler::TryHereOnly(this=0x4be2a68, event=...) (../../src/common/event.cpp:1585)
#11 007C50A0    wxEvtHandler::TryBeforeAndHere(this=0x4be2a68, event=...) (../../include/wx/event.h:3671)
#12 0062E157    wxEvtHandler::ProcessEventLocally(this=0x4be2a68, event=...) (../../src/common/event.cpp:1522)
#13 0062E0FF    wxEvtHandler::ProcessEvent(this=0x4be2a68, event=...) (../../src/common/event.cpp:1495)
#14 0062DCEC    wxEvtHandler::ProcessPendingEvents(this=0x4be2a68) (../../src/common/event.cpp:1359)
#15 0063A69C    wxAppConsoleBase::ProcessPendingEvents(this=0x4b2bde0) (../../src/common/appbase.cpp:520)
#16 007F0883    wxIdleWakeUpModule::MsgHookProc(nCode=0, wParam=1, lParam=77720172) (../../src/msw/window.cpp:7454)
#17 746BE1A1    USER32!TrackMouseEvent() (C:\WINDOWS\SysWOW64\user32.dll:??)
#18 ??  ?? () (??:??)

#2突出显示为红色。

也许它与SetPayload()中的Clone()部分有关?虽然我不太明白我应该如何使用它,或者为什么我访问有效载荷会有问题......

1 个答案:

答案 0 :(得分:1)

您不能使用指向局部变量的指针,当您退出包含它的函数时,该指针将被销毁,从而使指针无效,作为有效负载。而是使用对象本身,而不是指向它的指针。

相关问题