Binding events in one class from another? Topic is solved

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
ouch67
Earned some good credits
Earned some good credits
Posts: 134
Joined: Sun Mar 23, 2008 12:09 am

Binding events in one class from another?

Post by ouch67 » Tue May 01, 2012 10:59 pm

So I'm trying to create a function that can create a new timer and auto bind events for it. The problem is that the timer only works within that function and not in the function/class which called it. You can uncomment the loop in "newtimer" an you can see the timer works fine until the end of the function.

Code: Select all

void OnTimer(wxTimerEvent& event) //gets called each timer event
{
   wxString writeout;
   writeout.Printf("timer %i says hi", event.GetId());
   wxMessageBox(writeout);
}

wxTimer* newtimer(wxEvtHandler *eventhandler, void (*the_function) (wxTimerEvent&) = NULL)
{
    eventhandler->Bind(wxEVT_TIMER, the_function);
    wxTimer m_timer(eventhandler);
    m_timer.Start(2000);
    /*
    for (int x=0; x<10 ;x++)
    {
        Sleep(300);
        wxYield();
    }
    */
    return &m_timer;
}

void MyDialog::OnButton5Click(wxCommandEvent& event)
{
    wxTimer* m_timer = gauges.newtimer(this, OnTimer);
   //does a few more file processing loops here
}
Any help would be appreciated. I also tried starting the timer again in OnButton5Click but still nothing... It's probably something obvious I'm sure, but I've been staring at code all day... :)

User avatar
doublemax
Moderator
Moderator
Posts: 14518
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Binding events in one class from another?

Post by doublemax » Tue May 01, 2012 11:05 pm

You're creating the timer on the stack and you're returning a pointer to a local variable (which will be destroyed automatically).

I'm surprised this didn't crash.
Use the source, Luke!

ouch67
Earned some good credits
Earned some good credits
Posts: 134
Joined: Sun Mar 23, 2008 12:09 am

Re: Binding events in one class from another?

Post by ouch67 » Thu May 03, 2012 9:32 pm

Damn it your right as usual... :)

I did this exact same thing to another variable in another function that was giving me some weird issues too...

Post Reply