romatthe
romatthe

Reputation: 1477

C# - Application terminates unexpectedly without leaving traces

I have a simple C# application/game using SFML.Net that currently stops running / terminates within 1 or 2 seconds after being executed without any warning, exception, etc. whatsoever. Consider the following code:

public static void Main(string[] args)
{
   AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);

   --> The following two lines are run
   Console.WriteLine("Hello");
   Console.WriteLine("Please don't go...");

   // Run the game
   try
   {
      --> This line is reached
      Game.Run();
   }
   catch (Exception e)
   {
      --> This line is never reached
      Console.WriteLine(e.Message.ToString());
   }

   --> These lines are never reached
   Console.WriteLine("Noone ever comes here, I feel so lonely... :(");
   Console.ReadKey();
   }
}

At this point you probably expect that something is wrong with the Game.Run() method. The odd thing is that the first line of the method is never reached according to the VS Debugger.

public static void Run()
{
    try
    {
        --> Never reached
        LoadContent();

        // Do stuff here

        while (window.IsOpen())
        {
            // The classic game loop
        }
    }
    catch (Exception e)
    {
        --> Never reached
        Console.WriteLine(e.Message.ToString());
        Console.ReadKey();
    }
}

Upvotes: 3

Views: 2817

Answers (4)

romatthe
romatthe

Reputation: 1477

I came back to this after a couple of months and realized how silly my mistake was. Apparently unmanaged code debugging wasn't enabled by default (this was my first time using VS2012), and this exception was being thrown from the base C++ libs of SFML.

For those who don't know, to enable unmanaged debugging: Project > Properties > Debug > Select "Enable unmanaged code debugging"

Upvotes: 1

Sergey Vlasov
Sergey Vlasov

Reputation: 27910

You can get all called functions stack in your application until the termination point with the Runtime Flow tool (30 day trial, developed by me) and find what actually happens.

Upvotes: 1

Vinod Kumar Y S
Vinod Kumar Y S

Reputation: 628

My Guess is that the exception is being thrown by another thread. You cannot catch an exception thrown from another thread in the main thread. So your breakpoint in catch will never be hit. Btw did you try having a breakpoint in your UnhandledExceptionEventHandler method OnUnhandledException? That is where all unhandled exceptions go! Hope this helps.

Upvotes: 1

Avi Turner
Avi Turner

Reputation: 10456

My guess is that either:

OR

  • There is some other exception before you go into the Run method that is not handled. In order to verify that, make sure your debugger stops on first chance exceptions.

It would also be helpful if you specify:

  1. How do you know that the first line is never reached? breakpoint? output?
  2. What happens when you expect this function to be entered? does your application exist?
  3. Are you debugging step by step?

Upvotes: 1

Related Questions