Appcrash on shutdown and exception handling

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
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

Appcrash on shutdown and exception handling

Post by Natulux »

Hey there

I experienced some unwanted appcrashs, when windows (7 or 10) forces my application to close on shutdown.
Using the documentation and older posts, I came up with the following code:

Code: Select all

EVT_END_SESSION(MyApp::OnSystemShutdown)
EVT_QUERY_END_SESSION(MyApp::OnSystemEndSession)

bool MyApp::OnExceptionInMainLoop()
{
	try
	{
		throw;
	}
	catch (const wxString & str)
	{
		DoLog("OnExceptionInMainLoop() error: " + str, true);
		throw;
	}
	catch (const std::exception& e)
	{
		wxString sErr = "";
		sErr << "OnExceptionInMainLoop() error: " << e.what();
		DoLog(sErr, true);
		throw;
	}
	catch (...)
	{
		DoLog("OnExceptionInMainLoop unknown error", true);
		throw;
	}

	return true;
}

void MyApp::OnUnhandledException()
{
	wxString sErr = "";
	sErr << "Unhandled Exception thrown, wxSysErrorCode: " << wxSysErrorCode();
	DoLog(sErr, true);
}

void MyApp::DoLog(wxString logstring, bool bForceLog)
{
	bool bLogging = false;

	if (bForceLog)
	{
		bLogging = true;
	}
	else if (mb_writelogfile)
	{
		bLogging = true;
	}

	if(bLogging)
	{
		wxLogNull logNo;
		wxFileName fn_logfile = wxStandardPaths::Get().GetExecutablePath();
		fn_logfile.SetExt("log");

		wxString datetime = wxDateTime::Now().Format();
		wxString buf = "";
		buf << datetime << " - " << logstring;

		wxTextFile file(fn_logfile.GetFullPath());
		if(!fn_logfile.FileExists())
		{
			file.Create();
		}
		if(file.Open())
		{
			while(file.GetLineCount()>500)
			{
				file.RemoveLine(0);
			}
			file.AddLine(buf);				
			/*Datei speichern*/
			file.Write();
			file.Close();
		}
	}
}
I supposed this would redirect the error message of the crash, so that the user doesn't see it anymore but I could log it away.

Furthermore I noticed, that a the error message on a appcrash in normal windows running mode is also caught, but I get no logging message. It just results in a silent closing of my app.
I tried other ways of displaying a message in this case to no avail (eg. wxMessageBox, wxMeesageDialog) so im not even sure, if the end process is even running through this routine. All im noticing is, that if I handle exceptions of the main loop myself, I get no standard error messages whatsoever.

In fact, the only way to get a proper message out was when I provoked a std::exception in a try catch block and threw that myself.
Am I missing something here? Do I need to catch the exception in every class and if so, what would be the function for that?
Or do I really need to place every function in a try catch block to get proper exception handling? :-(

I am fine with a message, when windows is running and I want to suppress messages, when windows is shutting down.

Thank you.

Greets
Natu
User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: Appcrash on shutdown and exception handling

Post by eranon »

Hello, For me, when you rethrow from your override of OnExceptionInMainLoop, you obtain the standard Windows message: it's the "normal" behavior. It simply replaces the message provided by wxWidgets by another. Maybe a useful link in case you don't saw it: http://docs.wxwidgets.org/3.0/overview_exceptions.html
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Appcrash on shutdown and exception handling

Post by doublemax »

Maybe i'm too idealistic, but what about fixing the cause of the crash?
Use the source, Luke!
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

Re: Appcrash on shutdown and exception handling

Post by Natulux »

@eranon
Thanks for the link. I had made use of it before and build my app accordingly.
you need to build it with wxUSE_EXCEPTIONS set to 1
I made sure this is set.
To summarize, when you use exceptions in your code, you may handle them in the following places, in order of priority:
1. In a try/catch block inside an event handler.
2. In wxApp::OnExceptionInMainLoop().
3. In wxApp::OnUnhandledException().
I use all three of them. In 1. I rethrow the message with further description. Overwriting 2. and 3. should and does result in surpressing the standard error messages. My problem is, that I am unable to post my own error messages on appcrash instead of the standard error message. No Log written, no wxMessageBox or wxMessageDialog is shown and that is why I wasn't sure, if my code even passes through there.


@doublemax
:D
I don't think you are to idealistic here. And I'm going to try to solve the crash. But this is a good chance to handle further unknown errors and exceptions, for which I would like to be prepared.


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

Re: Appcrash on shutdown and exception handling

Post by doublemax »

BTW: Under Windows i use CrashRpt:
http://crashrpt.sourceforge.net/

It doesn't suppress errors, but it gives you the chance to send a bug report and crash dump back to you with very little effort. It can also add a screenshot at the time of the crash or even a video of a configurable time before the crash! The latter of course eats some CPU time and should only be enabled as a last resort :)
Use the source, Luke!
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

Re: Appcrash on shutdown and exception handling

Post by Natulux »

Hm, sounds gorgeous! I often would love to have more information about several errors occuring. ;-)
Though, in this particular case, I don't think I could use complex error handling like that in the 1 or 2 seconds of time, that windows grants me before killing the app right away (on shutdown).

Im not even sure, how it works at all.

Code: Select all

void MyApp::OnSystemEndSession(wxCloseEvent& event)
{
	DoLog("OnSystemEndSession - Windows is about to shutdown");
	
	if (event.CanVeto())
	{
		event.Veto();
		return;
	}

	event.Skip();
}

void MyApp::OnSystemShutdown(wxCloseEvent& event)
{
	DoLog("OnSystemShutdown - Windows shutdown");
}
I use this code to buy some more time when windows is shutting down, and the destructor of my main frame is called automatically it seems.
I even needed to shift some critical timers from MyApp to MyFrame, so I had some control over them beeing stopped and deleted at all.

Stopping a timer in said function above immediatly crashed the whole thing. Maybe the pointer was already deleted at that point.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Appcrash on shutdown and exception handling

Post by ONEEYEMAN »

Hi,
Natulux wrote:Hm, sounds gorgeous! I often would love to have more information about several errors occuring. ;-)
Though, in this particular case, I don't think I could use complex error handling like that in the 1 or 2 seconds of time, that windows grants me before killing the app right away (on shutdown).

Im not even sure, how it works at all.

Code: Select all

void MyApp::OnSystemEndSession(wxCloseEvent& event)
{
	DoLog("OnSystemEndSession - Windows is about to shutdown");
	
	if (event.CanVeto())
	{
		event.Veto();
		return;
	}

	event.Skip();
}

void MyApp::OnSystemShutdown(wxCloseEvent& event)
{
	DoLog("OnSystemShutdown - Windows shutdown");
}
I use this code to buy some more time when windows is shutting down, and the destructor of my main frame is called automatically it seems.
I even needed to shift some critical timers from MyApp to MyFrame, so I had some control over them beeing stopped and deleted at all.

Stopping a timer in said function above immediatly crashed the whole thing. Maybe the pointer was already deleted at that point.
And you just forgot to set it to NULL... ;-)

Thank you.
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

Re: Appcrash on shutdown and exception handling

Post by Natulux »

I hope you had a nice holiday! :-)
ONEEYEMAN wrote:And you just forgot to set it to NULL... ;-)
What am I supposed to set NULL? The event is no pointer and Im either skipping or vetoing it. I don't see what you mean ^^

Have a nice day
Natu
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

Re: Appcrash on shutdown and exception handling

Post by Natulux »

If anyone else also had appcrashes on windows shutdown, catching these events solved it for me on Windows 10 (I couldn't get my Win7 machine to get the error at all):

Code: Select all

EVT_END_SESSION(MyApp::OnSystemShutdown)
EVT_QUERY_END_SESSION(MyApp::OnSystemEndSession)

void MyApp::OnSystemEndSession(wxCloseEvent& event)
{
	DoLog("OnSystemEndSession - Windows is about to shutdown");
	
	//this buys the needed time for the frame to end
	if (event.CanVeto())
	{
		event.Veto();
		return;
	}

	event.Skip();
}

void MyApp::OnSystemShutdown(wxCloseEvent& event)
{
	DoLog("OnSystemShutdown - Windows shutdown");
	//Skipping this leads to direct system shutdown (despite the veto before) and to the incorrect memory access
	//event.Skip(); 
}
I am still hassling with the exception handling though.

Greets Natu
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

Re: Appcrash on shutdown and exception handling

Post by Natulux »

doublemax wrote:BTW: Under Windows i use CrashRpt:
http://crashrpt.sourceforge.net/

It doesn't suppress errors, but it gives you the chance to send a bug report and crash dump back to you with very little effort. It can also add a screenshot at the time of the crash or even a video of a configurable time before the crash! The latter of course eats some CPU time and should only be enabled as a last resort :)
Now I learned again, why one would like to have more than just Visual Studio Essentials. CrashReport needs ATL lib, which is not included in Essentials. There even is a MFC-Demo included, which of course also doesn't work without MFC.
Since my employer doesn't see the need to equip his employees with professional editions yet, I may end up buying it myself. :-/
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Appcrash on shutdown and exception handling

Post by ONEEYEMAN »

Hi,
Just remember - it M$, not MS. Therefore all their products are pricy. ;-)

Thank you.
Post Reply