Secure App restart and exception on timer destruction  [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.
Natulux
Earned some good credits
Earned some good credits
Posts: 110
Joined: Thu Aug 03, 2017 12:20 pm

Secure App restart and exception on timer destruction

Postby Natulux » Fri May 18, 2018 8:42 am

Hey guys,

I would like to implement a safe "CloseAndRestartWhenYouCan" routine into my project. The project is running several wxTimer-Loops, so I had some issues before when closing the app, because a timer-loop may still be doing fancy things.
I thought about a routine which wouldn't allow a timer to re-Start(), when an app-restart is requested so they could finish the work before closing. (I stop my timers before doing the work and usually restart them after it).

I derived wxTimer for that purpose to intercept the Start() function, which is ok.

Code: Select all

bool MyTimer::Start(int milli, bool oneShot)
{
   try
   {
      if (!wxGetApp().SetForSecureRestart())
      {
         return wxTimer::Start(milli, oneShot);
      }
   }
   catch (...)
   {
      return wxTimer::Start(milli, oneShot);
   }
   return true;
}


SetForSecureRestart() will close the app, when all timers stopped running:

Code: Select all

bool MyApp::SetForSecureRestart()
{
   bool bRestart = true;

   if (mb_secureAppRestart)
   {
      try
      {
         if (m_frame->mt_GOtimer->IsRunning())
         {
            bRestart = false;
         }
         
         if (m_frame->mt_notifTimer->IsRunning())
         {
            bRestart = false;
         }
         
         if (m_frame->mt_settingsTimer->IsRunning())
         {
            bRestart = false;
         }
         
         if (m_frame->m_watchfolder->IsRunning())
         {
            bRestart = false;
         }         
      }
      catch (...)
      {
      }
   }

   if (bRestart && mb_secureAppRestart)
   {
      RestartApp(false);
   }
   return mb_secureAppRestart;
}


But with the derived timer, I get an exception when destroying the timer:

Code: Select all

MyFrame::~MyFrame()
{
   try
   {
      mt_GOtimer->Stop();
      ...
      -->wxDELETE(mt_GOtimer);
      ...

leading to

Code: Select all

wxTimer::~wxTimer()
{
    -->Stop();

    delete m_impl;
}

resulting in "abort.cpp"

Code: Select all

   if (__abort_behavior & _CALL_REPORTFAULT)
    {
        #if defined _M_ARM || defined _M_ARM64
        __fastfail(FAST_FAIL_FATAL_APP_EXIT);
        #else
        if (IsProcessorFeaturePresent(PF_FASTFAIL_AVAILABLE))
            --> __fastfail(FAST_FAIL_FATAL_APP_EXIT);

        __acrt_call_reportfault(_CRT_DEBUGGER_ABORT, STATUS_FATAL_APP_EXIT, EXCEPTION_NONCONTINUABLE);
        #endif
    }


Do you by any chance know, why I would get an exception on Stop() in the destructor? The timer is no nullptr yet, according to my vs2015 debugger.
Or do you know a better way to solve my restart issue?

Have a nice day
Natu

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

Re: Secure App restart and exception on timer destruction  [SOLVED]

Postby doublemax » Fri May 18, 2018 10:12 am

I don' t know why it crashes, but from an OOP point of view that's a horrible solution. You have all the timers in your application hard-coded in MyApp::SetForSecureRestart().

First of all, you shouldn't need any timer management at all unless the different timers depend on each other.

But if you decide to implement some kind of timer management, i would use something like a "TimerManager" class which should be implemented as a singleton. A special timer class derived from wxTimer, e.g. "ManagedTimer" would register/unregister themselves automatically in the TimerManager on construction/destruction.

Then you would have a TimerManager::Stop() method that iterates over all timers, Stop()s them and waits until all are stopped.
Use the source, Luke!

Natulux
Earned some good credits
Earned some good credits
Posts: 110
Joined: Thu Aug 03, 2017 12:20 pm

Re: Secure App restart and exception on timer destruction

Postby Natulux » Fri May 18, 2018 1:01 pm

Do you have further knowledge on how Timer::Stop works? Is the code of a timer event still executed, when the timer is stopped from outside or is the whole event being interrupted?

Im starting to think that the whole business of timer surveillance isn't needed here at all, if I just can stop them all and wait for them... ;-)

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

Re: Secure App restart and exception on timer destruction

Postby doublemax » Fri May 18, 2018 1:26 pm

Stop() means that the timer will not generate any more events. And as there is only one thread involved, a timer event can not be running when you call Stop() from somewhere else.
Use the source, Luke!

Natulux
Earned some good credits
Earned some good credits
Posts: 110
Joined: Thu Aug 03, 2017 12:20 pm

Re: Secure App restart and exception on timer destruction

Postby Natulux » Fri May 25, 2018 6:07 am

You're right. AFAICS my approach wasn't necessary at all. Thanks.


Return to “C++ Development”

Who is online

Users browsing this forum: No registered users and 16 guests