[msw 2.8.7] Problem saving application settings when system is shut down

Vadim Zeitlin vadim at wxwidgets.org
Mon Apr 14 12:48:05 PDT 2008


On Mon, 14 Apr 2008 21:30:38 +0200 Eric Jensen <ml at j-dev.de> wrote:

EJ> Friday, April 11, 2008, 7:54:46 PM, you wrote:
EJ> 
EJ> VZ>  So we clearly have to destroy all the existing windows from either
EJ> VZ> wxWindowMSW::HandleEndSession() or, if we want this to be configurable,
EJ> VZ> from wxEVT_END_SESSION handler in wxApp. If you (or anybody) else can write
EJ> VZ> a patch adding such handler, it would be great. Of course, writing it
EJ> VZ> should be simple enough, it's the testing part which risks to be
EJ> VZ> time-consuming.
EJ> 
EJ> i tried to implement what you described and looked through the
EJ> wx-sources if there was already a function that did that and found
EJ> wxApp::CleanUp(). I tried the following change:

 Hello,

 Thanks for looking at this!

EJ> void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
EJ> {
EJ>   if (GetTopWindow())
EJ>     GetTopWindow()->Close(true);
EJ> 
EJ>   // two new lines here
EJ>   this->CleanUp();
EJ>   this->ExitMainLoop();
EJ> }
EJ> 
EJ> This seems to work fine in the sample i tried and one of my own,
EJ> bigger and more complex application.

 From looking at the code it looks like we should rather be calling
OnExit() and then wxEntryCleanup() here -- at least this is what's done
during the normal application termination and so should fit the
expectations best.

 OnExit() should be called to execute any cleanup code the user program
might put there and wxEntryCleanup() will call wxApp::CleanUp() and do a
few more things.

EJ> However i don't know the internal workings of wxwidgets well enough to
EJ> estimate any possible side-effects of this code. And i'm a little
EJ> concerned about what would happen, if Windows - for whatever reason -
EJ> does not forcefully kill the process. In that case i'd be cutting the
EJ> tree i'm sitting on by destroying all toplevel windows there (because
EJ> the code is in effect called by an event handler in wxTopLevelWindow)

 Normally the code should work fine when the event handler destroys the
object it was called on. However to make it totally "safe" we could call
exit() ourselves from here. This would have a (big) added benefit of
executing dtors of global objects too.

 Could you please check if

	void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
	{
		// Windows will terminate the process soon after we
		// return from WM_ENDSESSION handler anyhow, so make
		// sure we at least execute our cleanup code before
		const int rc = OnExit();

		wxEntryCleanup();

		// calling exit() instead of ExitProcess() or not doing
		// anything at all and being killed by Windows has the
		// advantage of executing the dtors of global objects
		exit(rc);
	}

works correctly?

 Thanks!
VZ

-- 
TT-Solutions: wxWidgets consultancy and technical support
               http://www.tt-solutions.com/



More information about the wx-users mailing list