Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Predicting that the program will crash

Tags:

c++

c#

crash

I've been using Google Chrome for a while now and I noticed that it features very elegant crash control.

Just before crashing, google chrome gave a message saying "Woah! Google Chrome has crashed. Restart now?". And right after, I'd get a standard Windows XP "This program has encountered a problem and needs to close." with the "Debug", "Don't send" and "Send Error Report" buttons.

My question is how can you program your compiled application to detect a crash condition in advance? If you have knowledge of how to do it in any programming language / platform would be great.

Thanks

like image 882
Wadih M. Avatar asked Jun 12 '09 20:06

Wadih M.


People also ask

How do you predict a crash?

A high price increase over the past 6 to 12 months increases the likelihood of a predicted crash, indicating that a general price increase over the long term makes a crash more likely and that price movements over longer time periods contain valuable information for crash forecasting.

What causes a program to crash?

Typical causes include accessing invalid memory addresses, incorrect address values in the program counter, buffer overflow, overwriting a portion of the affected program code due to an earlier bug, executing invalid machine instructions (an illegal opcode), or triggering an unhandled exception.

What happens when a program crashes?

A crash is when a software program stops working and then closes. When this occurs, Windows usually creates an entry in the event log to help diagnose the problem. A hang is when a software program becomes unresponsive.

How do I force an app to crash on Windows 10?

To quickly force quit on Windows, use the keyboard shortcut Alt + F4. Make sure the app or program window is open when you click Alt + F4. You can also force quit on Windows by using the Task Manager or Command Prompt.


3 Answers

Google Chrome uses a technique (often called process separation) where the 'host' UI manages child processes that it can detect becoming unresponsive (or worse, throwing an error and closing). It starts a new process for each tab you open.

Here's an article describing this in a bit more detail.

Using .net's Process class, you can start processes, check if they're responsive and even kill them.

If you want to embed a process' window within your own, you can use platform functions such as SetParent to move one window within another. I'm afraid I'm not aware of a managed alternative to SetParent when working with Forms, but I suspect one exists so it's worth searching for that before using SetParent.

If the host process crashes, simply using something like AppDomain.CurrentDomain.UnhandledException will allow you to receive a notification that a top-level exception has occurred, but by this stage you're unlikely to be able to predict the state of objects within your app and restarting (along with some logging and a notification to the user) is probably your only sensible option.

Handling top-level exceptions is covered in detail here.

like image 163
Matt Brindley Avatar answered Oct 06 '22 03:10

Matt Brindley


I don't know Google's code so I am speculating. Google Chrome probably isn't predicting that it will crash, but detecting that it has crashed.

In Windows you can do this by providing a handler for all unhandled exceptions. In this handler you might do things such as restart the application, create a minidump file, etc.

Take a look at SetUnhandledExceptionFilter Function for one method.

like image 41
John Dibling Avatar answered Oct 06 '22 02:10

John Dibling


In .NET, you can hook up to the System.AppDomain.CurrentDomain.UnhandledException event. Your code would look something like this:

using System;
using System.Windows.Forms;

public class Program
{
    public static void Main(string[] args)
    {
        AppDomain.CurrentDomain.UnhandledException += MyUnhandledExceptionHandler;

        // start rest of application
    }

    private static void MyUnhandledExceptionHandler(object sender, EventArgs args)
    {
        MessageBox.Show("Your app is crashing.  Watch out!");
    }
}
like image 43
skb Avatar answered Oct 06 '22 03:10

skb