Reputation: 435
I have a C++ program in Code::Block with gcc and wxWidgets.
After my working thread throws a wxThreadEvent
with a struct as payload my program crashes (actually not at the throw, but at the moment I want to get the payload in the main).
Does anyone have an idea what is wrong?
The working thread part:
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);
}
The struct:
struct position_variables{
double latitude;
double longitude;
wxString direction;
};
class NavigationThread : public wxThread
{
...
}
The 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
}
When the crash occurs in normal "run" mode, a windows opens saying the program stopped working. In debug mode there is a small window in Code::blocks saying something about SIGSEGV, segmentation fault
(or something like that) and this is the Call Stack:
#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 ?? ?? () (??:??)
with #2 highlighted red.
Maybe it has something to do with the Clone()
part in SetPayload()
? Though I don't quite get how I should use it or why my accessing of the payload would be problematic...
Upvotes: 0
Views: 333
Reputation: 22688
You can't use a pointer to a local variable, which will be destroyed as soon as you exit the function containing it, thus making the pointer invalid, as the payload. Use the object itself, not a pointer to it, instead.
Upvotes: 1