9

My application throws some strange errors if you shut down the computer while my application is running.

Sometimes the message is (address) memory can not be "read", sometimes can not be "write".

Shutting down the application in the normal way doesn't generate such messages.

How can I simulate the "windows shutdown" so that I can debug my application? How can I find out what the application is trying to do that it cannot?

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
Neka
  • 1,574
  • 4
  • 22
  • 36
  • 1
    If your application doesn't respond to to the shutdown and kill signals and events, then your program will be terminated no matter what it's currently doing. And if the system is going down either way, does it really matter what it does with its memory? It won't be save anyway. – Some programmer dude Jun 23 '15 at 09:38
  • Windows sends `WM_QUIT` messages around when shutting down, so you could test what happens when you send this to your app. – Jongware Jun 23 '15 at 09:56

1 Answers1

7

When Windows wants to shutdown, it sends a series of events to the application; such as WM_ENDSESSION and WM_QUIT. You can process these in the message handler you are using; in general the application will need to respond appropriately and quickly to these messages else the OS will just terminate the application anyway. I'm not sure what default processing wxwidgets offers in this regard. Hooking into these would help in diagnosing the application error itself.

There are a few things you could attempt to do;

  • The shutdown sequence will not be easy to simulate (if at all) - a lot happens during shutdown; the exact state and situation is difficult to simulate in it's entirety.
  • In terms of diagnosing the state of the application just before shutdown, you could try to process the WM_QUERYENDSESSION and respond with a FALSE to prevent it from shutting down (with newer versions of Windows you can no longer prevent the shutdown, so it may not work depending on the platform you are on).
  • You could also try to test the application's immediate response to WM_ENDSESSION message by sending it the WM_ENDSESSION (e.g. via a PostMessage) with the appropriate data as detailed on MSDN.

For terminal based applications; You can also hook in the signals (SIGKILL I believe) if required. See this Microsoft reference for more detail. You can also the the SetConsoleCtrlHandler hook. But since you using a toolkit, it would be better to use the messages sent to the application already.

Niall
  • 30,036
  • 10
  • 99
  • 142
  • 2
    Simulating shutdown is not that easy, in particular because modern Windows versions just kill the application if it doesn't return quickly enough. The best way to test this is to run your application inside a VM and really shut it down. – VZ. Jun 23 '15 at 13:27
  • @VZ. I agree, there is no way to fully "simulate" the shutdown, these are all really just hints at what the OP could do to try diagnose the issue. I'll edit it to try make that more clear. – Niall Jun 23 '15 at 13:33
  • @VZ, ok, I got it. But error messages comes from my app, when it tries to do some inappropriate things with the memory. So, it doesn't killed by the OS at this moment. It just recieved signal from OS to shut down – Neka Jun 23 '15 at 15:58
  • 1
    In modern versions of Windows, returning FALSE from WM_QUERYENDSESSION no longer prevents the system from shutting down. – Adrian McCarthy Jun 23 '15 at 23:33
  • That rings a bell @AdrianMcCarthy, forgot about that, thanks. Added some clarity to the post. – Niall Jun 24 '15 at 06:55