wxTimer m_Time cause crash

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
oldrealrealman
In need of some credit
In need of some credit
Posts: 5
Joined: Tue Nov 27, 2018 2:44 am

wxTimer m_Time cause crash

Post by oldrealrealman »

codes is as following
wxTimer m_Time; in headfile

:m_Timer(this, TIMER_ID)

m_Timer.Start(1000, wxTIMER_CONTINUOUS );


in debug version it is ok, but in realease version, it crashed.

Code 17.12
wxWidgets 3.1.1

if adding m_Timer->Stop in onPaint function then all of versions are ok.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxTimer m_Time cause crash

Post by doublemax »

That looks ok so far.

What platform are you using?

When does it crash? When the first timer event fires?

Can you show some more code in context?
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7479
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxTimer m_Time cause crash

Post by ONEEYEMAN »

Hi,
Does it give you an assert or the hard crash?
Can you trace the code - even in Release build you can do some debugging?

Thank you.
oldrealrealman
In need of some credit
In need of some credit
Posts: 5
Joined: Tue Nov 27, 2018 2:44 am

Re: wxTimer m_Time cause crash

Post by oldrealrealman »

I'm using win10.
very simple codes, just to create new project with CodesBlocks wizard. The codes is as following

Code: Select all

/***************************************************************
 * Name:      test1Main.cpp
 * Purpose:   Code for Application Frame
 * Author:     ()
 * Created:   2018-11-28
 * Copyright:  ()
 * License:
 **************************************************************/

#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif

#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__

#include "test1Main.h"

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__WXMAC__)
        wxbuild << _T("-Mac");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

const long test1Frame::idMenuStart = wxNewId();
#define TIMER_ID 1234

BEGIN_EVENT_TABLE(test1Frame, wxFrame)
    EVT_CLOSE(test1Frame::OnClose)
    EVT_MENU(idMenuQuit, test1Frame::OnQuit)
    EVT_MENU(idMenuStart, test1Frame::OnMenuStartSelected)
    EVT_MENU(idMenuAbout, test1Frame::OnAbout)
END_EVENT_TABLE()

test1Frame::test1Frame(wxFrame *frame, const wxString& title)
    : wxFrame(frame, -1, title),
     m_Timer(this, TIMER_ID)
{
#if wxUSE_MENUS
    // create a menu bar
    wxMenuBar* mbar = new wxMenuBar();
    wxMenu* fileMenu = new wxMenu(_T(""));
    fileMenu->Append(idMenuQuit, _("&Quit\tAlt-F4"), _("Quit the application"));
    fileMenu->Append(idMenuStart, _("&Start\tAlt-F6"), _("Start the application"));
    mbar->Append(fileMenu, _("&File"));



    wxMenu* helpMenu = new wxMenu(_T(""));
    helpMenu->Append(idMenuAbout, _("&About\tF1"), _("Show info about this application"));
    mbar->Append(helpMenu, _("&Help"));

    SetMenuBar(mbar);
#endif // wxUSE_MENUS);
    Connect(wxEVT_TIMER, (wxObjectEventFunction)&test1Frame::OnTimer);
#if wxUSE_STATUSBAR
    // create a status bar with some information about the used wxWidgets version
    CreateStatusBar(2);
    SetStatusText(_("Hello Code::Blocks user!"),0);
    SetStatusText(wxbuildinfo(short_f), 1);
#endif // wxUSE_STATUSBAR

}


test1Frame::~test1Frame()
{
    m_Timer.Stop();
}
void test1Frame::OnTimer()
{
}

void test1Frame::OnMenuStartSelected(wxCommandEvent& event)
{
    m_Timer.Start(1000);
}

void test1Frame::OnClose(wxCloseEvent &event)
{
    Destroy();
}

void test1Frame::OnQuit(wxCommandEvent &event)
{
    Destroy();
}

void test1Frame::OnAbout(wxCommandEvent &event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}
Last edited by doublemax on Wed Nov 28, 2018 10:39 am, edited 1 time in total.
Reason: added code tags
oldrealrealman
In need of some credit
In need of some credit
Posts: 5
Joined: Tue Nov 27, 2018 2:44 am

Re: wxTimer m_Time cause crash

Post by oldrealrealman »

I don't know how to get the useful information from the trace, the following is the trace infor which I got from gdb.


Debugger name and version: GNU gdb (GDB) 7.9.1
Child process PID: 34676
At C:\workcode\test1\test1Main.cpp:99
At C:\workcode\test1\test1Main.cpp:100
In wxAppConsoleBase::HandleEvent(wxEvtHandler*, void (wxEvtHandler::*)(wxEvent&), wxEvent&) const () (C:\wxWidgets-3.1.1\lib\gcc_dll\wxbase311u_gcc_custom.dll)
In wxAppConsoleBase::CallEventHandler(wxEvtHandler*, wxEventFunctor&, wxEvent&) const () (C:\wxWidgets-3.1.1\lib\gcc_dll\wxbase311u_gcc_custom.dll)
In wxEvtHandler::ProcessEventIfMatchesId(wxEventTableEntryBase const&, wxEvtHandler*, wxEvent&) () (C:\wxWidgets-3.1.1\lib\gcc_dll\wxbase311u_gcc_custom.dll)
In wxEventHashTable::HandleEvent(wxEvent&, wxEvtHandler*) () (C:\wxWidgets-3.1.1\lib\gcc_dll\wxbase311u_gcc_custom.dll)
In wxEvtHandler::TryHereOnly(wxEvent&) () (C:\wxWidgets-3.1.1\lib\gcc_dll\wxbase311u_gcc_custom.dll)
In wxEvtHandler::ProcessEventLocally(wxEvent&) () (C:\wxWidgets-3.1.1\lib\gcc_dll\wxbase311u_gcc_custom.dll)
In wxEvtHandler::ProcessEvent(wxEvent&) () (C:\wxWidgets-3.1.1\lib\gcc_dll\wxbase311u_gcc_custom.dll)
In wxWindowBase::TryAfter(wxEvent&) () (C:\wxWidgets-3.1.1\lib\gcc_dll\wxmsw311u_core_gcc_custom.dll)
In wxEvtHandler::SafelyProcessEvent(wxEvent&) () (C:\wxWidgets-3.1.1\lib\gcc_dll\wxbase311u_gcc_custom.dll)
In wxMenuBase::DoProcessEvent(wxMenuBase*, wxEvent&, wxWindow*) () (C:\wxWidgets-3.1.1\lib\gcc_dll\wxmsw311u_core_gcc_custom.dll)
In wxMenuBase::SendEvent(int, int) () (C:\wxWidgets-3.1.1\lib\gcc_dll\wxmsw311u_core_gcc_custom.dll)
In wxFrameBase::ProcessCommand(wxMenuItem*) () (C:\wxWidgets-3.1.1\lib\gcc_dll\wxmsw311u_core_gcc_custom.dll)
In wxFrame::HandleCommand(unsigned short, unsigned short, HWND__*) () (C:\wxWidgets-3.1.1\lib\gcc_dll\wxmsw311u_core_gcc_custom.dll)
In wxFrame::MSWWindowProc(unsigned int, unsigned int, long) () (C:\wxWidgets-3.1.1\lib\gcc_dll\wxmsw311u_core_gcc_custom.dll)
Cannot open file: ../../../../../src/gcc-5.1.0/libgcc/unwind-sjlj.c
At ../../../../../src/gcc-5.1.0/libgcc/unwind-sjlj.c:173
Cannot open file: ../../../../../src/gcc-5.1.0/libgcc/unwind-sjlj.c
At ../../../../../src/gcc-5.1.0/libgcc/unwind-sjlj.c:174
Cannot open file: ../../../../../src/gcc-5.1.0/libgcc/unwind-sjlj.c
At ../../../../../src/gcc-5.1.0/libgcc/unwind-sjlj.c:175
In wxFrame::MSWWindowProc(unsigned int, unsigned int, long) () (C:\wxWidgets-3.1.1\lib\gcc_dll\wxmsw311u_core_gcc_custom.dll)
In wxWndProc(HWND__*, unsigned int, unsigned int, long)@16 () (C:\wxWidgets-3.1.1\lib\gcc_dll\wxmsw311u_core_gcc_custom.dll)
In USER32!AddClipboardFormatListener () (C:\WINDOWS\System32\user32.dll)
In USER32!EndTask () (C:\WINDOWS\System32\user32.dll)
In USER32!GetScrollInfo () (C:\WINDOWS\System32\user32.dll)
In USER32!GetOpenClipboardWindow () (C:\WINDOWS\System32\user32.dll)
In USER32!GetScrollInfo () (C:\WINDOWS\System32\user32.dll)
In USER32!EndTask () (C:\WINDOWS\System32\user32.dll)
In USER32!IsWindow () (C:\WINDOWS\System32\user32.dll)
In USER32!EndTask () (C:\WINDOWS\System32\user32.dll)
In USER32!GetDC () (C:\WINDOWS\System32\user32.dll)
In USER32!EndTask () (C:\WINDOWS\System32\user32.dll)
In USER32!DispatchMessageW () (C:\WINDOWS\System32\user32.dll)
In wxGUIEventLoop::Dispatch() () (C:\wxWidgets-3.1.1\lib\gcc_dll\wxmsw311u_core_gcc_custom.dll)
In wxEventLoopManual::ProcessEvents() () (C:\wxWidgets-3.1.1\lib\gcc_dll\wxbase311u_gcc_custom.dll)
In wxEventLoopManual::DoRun() () (C:\wxWidgets-3.1.1\lib\gcc_dll\wxbase311u_gcc_custom.dll)
Program received signal SIGSEGV, Segmentation fault.
In ?? () ()
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxTimer m_Time cause crash

Post by PB »

It seems that test1Frame::OnTimer() method has a wrong signature, AFAIK the handler for wxEVT_TIMER should take a wxTimerEvent& parameter? I would check that part and make sure the Connect() gets correct parameters . I have never used Connect() or message tables, I use Bind() instead which seems much safer...

BTW, when posting a code, please:
(1) Remove all the useless stuff from it. Most of yours code is noise generated by a tool that has no relevance to the issue and only makes harder to read it. OTOH, it is not even usable for copy, paste & compile as it misses the code from header.
(2) Use code tags.

For example, this an example of smallest, self-contained code that demonstrates simple use of wxTimer

Code: Select all

#include <wx/wx.h>
#include <wx/timer.h>

class MyFrame : public wxFrame
{
public:
    MyFrame(): wxFrame(NULL, wxID_ANY, "Test")
    {
        wxTextCtrl* textCtrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString, 
            wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH2);             
        wxLog::SetActiveTarget(new wxLogTextCtrl(textCtrl));

        m_timer.Bind(wxEVT_TIMER, &MyFrame::OnTimer, this);        
        m_timer.Start(1000);
    }	
private:    
    wxTimer m_timer;
    
    void OnTimer(wxTimerEvent&)
    {        
        wxLogMessage("Timer");
    }
};

class MyApp : public wxApp
{
public:
    virtual bool OnInit()
    {     
        (new MyFrame())->Show();               
        return true;
    }
}; wxIMPLEMENT_APP(MyApp);
oldrealrealman
In need of some credit
In need of some credit
Posts: 5
Joined: Tue Nov 27, 2018 2:44 am

Re: wxTimer m_Time cause crash

Post by oldrealrealman »

Thank you. not only the answer, but also your instruction.

With your(above) codes, crash disapeared.
Trying as your suggestion, after updating 'void test1Frame::OnTimer()' with 'void test1Frame::OnTimer(wxTimerEvent&)', then it can running well.

Bind or connect, all of them are ok, you said Bind is safer, why?

But 'void test1Frame::OnTimer()' is also ok in debug version, not ok in release version , should be there is much different with debug and release lib/dll.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxTimer m_Time cause crash

Post by PB »

oldrealrealman wrote:Bind or connect, all of them are ok, you said Bind is safer, why?
Firstly, Bind() would not allow to use a method with a wrong signature. Secondly, it protects against using a wrong event sink accidentally, which is a common source of bugs.

Last but not least, Bind() has other advantages over Connect(): for example, you do not need to use the odd cast and you are not limited to using an wxEvtHandler as the event sink.

The above makes Bind() the recommended way of dynamic event handling. Connect() was used in wxWidgets 2.8 where Bind() was not available.
Post Reply