0

I have an application in C# for Windows Mobile 6.5 . The problem is that after a lot of time of use (2-3 hours), sometimes the app crashes with "NullReferenceException" in Main().

I have all the program with try catch, testing in main to catch different exceptions (ObjectDisposed, NullException and Exception), and also I've tried with the event:

AppDomain.CurrentDomain.UnhandledException +=
     new UnhandledExceptionEventHandler(OnUnhandledException);

The applications is still hanging after a time of use. Does anybody know any app to monitor this crashes like DebugDiag in Windows, or some piece of code to catch in a log this exception?

Code in main function is as follows:

 static void Main()
    {
        AppDomain.CurrentDomain.UnhandledException +=
                  new UnhandledExceptionEventHandler(OnUnhandledException);
        try
        {

            CLog.EscribirEnLog("Main");               

            using (AppExecutionManager execMgr = new AppExecutionManager(appName))
            {
                if (execMgr.IsFirstInstance)
                {

                    Application.Run(new FormInicioSesion());
                    CUtiles.MatarProceso("FoxitReader.exe");
                    CUtiles.MatarProceso("pimg.exe");
                }
            }
        }
        catch (NullReferenceException ex)
        {
            CLog.EscribirEnLog("ERROR Null " + ex.StackTrace);
            Application.Exit();
        }
        catch (ObjectDisposedException e)
        {
            CLog.EscribirEnLog("ERROR  : Excepcion en main : " + e.StackTrace.ToString() + ";   " + e.InnerException.ToString() + ";  " + e.Message.ToString());

            Application.Exit();
        }
        catch (Exception e)
        {
            CLog.EscribirEnLog("ERROR  : Excepcion : " + e.StackTrace.ToString() + ";   " + e.InnerException.ToString() + ";   " + e.Message.ToString());
            Application.Exit();

        }
        finally
        {
            CLog.EscribirEnLog("Main-Finally");
        }         

    }
Gonzalo
  • 1
  • 3
  • Thanks for your answer. I know what causes a NullReferenceException and ways to avoid it, the problem is that I don't know where is the problem, that's why I need a way to catch it to solve it. – Gonzalo May 25 '16 at 14:26
  • Your way to catch it would be with a `try...catch` block. Can you post what your `try...catch` code is? – Lews Therin May 25 '16 at 14:33
  • I have edited the post with the code – Gonzalo May 26 '16 at 13:22

1 Answers1

0

The only ways to get the source of the exception are:

  1. wrap all code that may throw an exception with try..catch. If using StackTrace and InnerException wrapping the main function with try catch is a good start
  2. use a log file to note the exception in the catch block
  3. use StackTrace and InnerException property to get detailed information on what the exception is about
  4. run your app in a debug session, possibly using remote debugging via network
josef
  • 5,951
  • 1
  • 13
  • 24
  • I've followed steps 1 to 3 and tried step 4, but remote debugging in windows mobile doesn't allow the mobile to connect to internet. – Gonzalo May 26 '16 at 13:24
  • If your mobile network setup allows an internet connection and remote debugging depends on the mobile setup. See Connect to the Internet/Work and set both to Work and the Work network to 'This connects to Internet'. Then set the network card to connect to Work. See the various infos about connection manager setup. – josef May 26 '16 at 17:35