wxSingleInstanceChecker (behavior change) and Auto Application Restart conflict?

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
User avatar
bsenftner
Experienced Solver
Experienced Solver
Posts: 62
Joined: Thu May 26, 2016 9:19 pm

wxSingleInstanceChecker (behavior change) and Auto Application Restart conflict?

Post by bsenftner » Mon Aug 12, 2019 6:41 pm

Developing with latest wxWidgets in Visual Studio 2015 C++ on Win10.

We have successfully been using wxSingleInstanceChecker in combination with the Win32 API RegisterApplicationRestart() to relaunch our wxWidgets application after crashes. However, we just found that Application Restart is no longer working. The problem has been tracked down to the wxSingleInstanceChecker saying the application is still running.

1) In the application's OnInit() handler a wxSingleInstanceChecker object is created
2) with that wxSingleInstanceChecker object, mp_singleInstChecker->IsAnotherRunning() is called.

Under a normal application launch, the IsAnotherRunning() test fails and the application proceeds to create the main window and launch.

After an application crash, when the IsAnotherRunning() test says another is already running, I've tried adding a delay and asking again... but after as much as a 20 second delay, calling IsAnotherRunning() from that same wxSingleInstanceChecker object is still returning "true".

It appears a new/different wxSingleInstanceChecker object needs to be created? I just tried adding the creation of a different wxSingleInstanceChecker object after my delay, and it confirms no other version is running. Any idea why or when this changed? Is this even correct, or does something smell fishy with this solution?

Code: Select all

bool Aureus3Dv ::OnInit() 
{
   mp_singleInstChecker = new wxSingleInstanceChecker;
   if ( mp_singleInstChecker->IsAnotherRunning() )
   {
		 // our crashed version might still be writing a crash recovery, give it time to complete:
		 wxSleep(20); // it only needs 1 second, but why is this returning true even after this long delay? 

		 delete mp_singleInstChecker; 
		 mp_singleInstChecker = new wxSingleInstanceChecker; // make a completely new instance? 

		 if ( mp_singleInstChecker->IsAnotherRunning() )
		 {
				wxMessageBox( "Multiple instances of this application were launched, this instance is auto-terminating!", 
											wxT(APP_NAME_LONG), wxOK | wxICON_EXCLAMATION, NULL );

				
				return false;
		 }
   }
	
	mp_mainframe = new Aureus3DvMainFrame(); 
	mp_mainframe->Show(true);

  return true;
}

/* Cleanup for Aureus3Dv */
int Aureus3Dv ::OnExit() { 
	
	if (mp_singleInstChecker)
	{
		delete mp_singleInstChecker; 
		mp_singleInstChecker = NULL;
	}

	return wxApp::OnExit();
}

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

Re: wxSingleInstanceChecker (behavior change) and Auto Application Restart conflict?

Post by doublemax » Mon Aug 12, 2019 7:40 pm

https://stackoverflow.com/a/14986952

Solution: Fix your app so that it doesn't crash (seriously).

You could also override wxAppConsole::OnFatalException() and check if it gets called on crash. Then you could destroy the wxSingleInstanceChecker instance from there.
https://docs.wxwidgets.org/trunk/classw ... 02ee6f6f9d
Use the source, Luke!

User avatar
bsenftner
Experienced Solver
Experienced Solver
Posts: 62
Joined: Thu May 26, 2016 9:19 pm

Re: wxSingleInstanceChecker (behavior change) and Auto Application Restart conflict?

Post by bsenftner » Mon Aug 12, 2019 8:45 pm

Thanks for the info. The primary type of crash we experience is users overloading their systems, which are then killed via Task Manager or similar. Our app does video analysis, and inexperienced users can overwhelm a system to unresponsiveness. So it's killed. There is little one can do with people who don't read manuals and just "try stuff". The typical person's idea of how much CPU analyzing video requires is off by a magnitude. To support such persons, we have a crash recovery manager that lets them recover what they were doing partially or in stages, in case the work was important, and recovery is required.

FWIW, OnFatalException() says one has to call wxHandleFatalExceptions(), which is somewhat difficult to know when in large, enterprise class applications.

Anyways, thanks again. Creating a second wxSingleInstanceChecker after a 3 second delay is working fine.

User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2259
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania
Contact:

Re: wxSingleInstanceChecker (behavior change) and Auto Application Restart conflict?

Post by evstevemd » Tue Aug 13, 2019 6:06 am

AFAIR, there is an option to specify name the checker lock file. So do that and learn where it stores that file (in *nix you can specify the path too, but you are on Windows), then delete it on restart after crash. Don't delete it under any other circumstance as it will render wxSIC useless.
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
[Ubuntu 19.04/Windows 10 Pro/MacOS 10.13 - GCC/MinGW/Clang, CodeLite IDE]

Post Reply