How to make C# application crash

后端 未结 12 2075
渐次进展
渐次进展 2020-12-31 13:00

I want to test if my application crash dump can be debugged. But firstly, I need to generate a crash dump of my application. I\'m using C# to code my app, and have tried wit

相关标签:
12条回答
  • 2020-12-31 13:45

    A surefire way to do it is as follows:

    ThreadPool.QueueUserWorkItem(new WaitCallback(ignored => 
    {
       throw new Exception();
    }));
    

    All the others can be handled by the top level ApplicationDomain.OnUnhandledException and the like.

    This one will kill it dead (assuming .NET 2.0+, and not using 'legacyUnhandledExceptionPolicy': http://msdn.microsoft.com/en-us/library/ms228965.aspx).

    0 讨论(0)
  • 2020-12-31 13:46

    Another option is to call

    System.Environment.FailFast("Error happened")
    
    0 讨论(0)
  • 2020-12-31 13:47

    None of the answers crashed my app the way I was looking for. So here is the approach that worked for me.

        private void Form1_Load(object sender, EventArgs e)
        {
            object p = 0;
            IntPtr pnt = (IntPtr)0x123456789;
            Marshal.StructureToPtr(p, pnt, false);
        }
    
    0 讨论(0)
  • 2020-12-31 13:49

    For C# in Unity3D

    There is UnityEngine.Diagnostics.Utils.ForceCrash (in Unity 2018.3)

    This can be used with one of the following ForcedCrashCategory enum parameter:

    AccessViolation

    Cause a crash by performing an invalid memory access.The invalid memory access is performed on each platform as follows:

    FatalError

    Cause a crash using Unity's native fatal error implementation.

    Abort

    Cause a crash by calling the abort() function.

    PureVirtualFunction

    Cause a crash by calling a pure virtual function to raise an exception.


    For older versions of Unity:

    UnityEngine.Application.ForceCrash(int mode)

    For even older versions (Unity 5):

    UnityEngine.Application.CommitSuicide(int mode)

    From my experience, mode 0 causes a "unity handled" crash (where the Unity crash dialog appears), and mode 2 causes a "hard" crash where the Windows error dialog appears.

    This seems consistent with this post by Smilediver on mode:

    0 - will simulate crash, 1 - will simulate a fatal error that Unity has caught, 2 - will call abort().

    (These methods are not documented as they were intended for Unity's internal use. They may also be marked [Obsolete] depending on your Unity version.)

    0 讨论(0)
  • 2020-12-31 13:55

    StackOverflowException is a badass:

    void PerformOverflow()
    {
      PerformOverflow();
    }
    

    Usage:

    PerformOverflow();
    
    0 讨论(0)
  • 2020-12-31 14:02
      public void Loop()
      {
          Loop();
      }
      //call this
      Loop();
    
    0 讨论(0)
提交回复
热议问题