How to make a c # application crash

I want to check if it is possible to debug a dump of my application, so I need to create a dump of my application first, but I write it using C # so that someone knows how to cause a crash. (In fact, I am testing many exceptions, unsafe code ......, but I don’t understand). thank you, sorry, sorry, I just lost something: I am creating an application with unity 3d that will handle exceptions for me automatically and will generate a crash failure for me if the application crashes

Thanks to everyone for your answers, I will just check all your methods in a common C # application and everything works, but not the only application written in C #, it seems that Unity3d does more, I think I need to send a unique3d letter to receive answer. I will send here if I receive it.

+9
source share
13 answers

The following will provide an unhandled exception and ask you to select a debugger:

System.Diagnostics.Debugger.Launch() 
+21
source

StackOverflowException is a badass:

 void PerformOverflow() { PerformOverflow(); } 

Using:

 PerformOverflow(); 
+14
source

Throw an exception :)

 throw new Exception("Your exception here!"); 
+11
source

The surefire way to do this is as follows:

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

All others can be handled by the top level ApplicationDomain.OnUnhandledException, etc.

This will kill him dead (it is assumed that .NET 2.0+ does not use 'legacyUnhandledExceptionPolicy': http://msdn.microsoft.com/en-us/library/ms228965.aspx ).

+5
source

Well. The only good 100% actual CLR error is to introduce a native exception into the managed world.

Calling Kernel32.dll RaiseException () directly will immediately cause ANY C # application and Unity Editor to crash.

 [DllImport("kernel32.dll")] static extern void RaiseException(uint dwExceptionCode, uint dwExceptionFlags, uint nNumberOfArguments, IntPtr lpArguments); void start() { RaiseException(13, 0, 0, new IntPtr(1)); } 

Happy crash. Note that for debugging the built-in and , you will need two instances of Visual Studio. If you are developing your own P / INVOKE plugin, configure it so that the Visual Studio 1 instance is a native debugger and uses Unity or your C # program as the host program, and you join the host program from another instance of Visual Studio.

+5
source

None of the answers crashed my application the way I was looking. 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); } 
+1
source
 public Object crashMe() { return null; } // call me! crashMe().ToString(); 

Shouldn't you just create a simple NullPointer and minimize the application?

0
source
  int[] x = {0}; int blah = x[2]; 

will also throw an exception

0
source
  public void Loop() { Loop(); } //call this Loop(); 
0
source

This is easy enough to reproduce if you try to convert a null game object. For example, like this:

 public static GameObject gameObjectCrash; public void GenerateCrash() { gameObjectCrash.transform.rotation = Quaternion.Euler(90, 0, 0); } 
0
source

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 parameters:

AccessViolation

Fail by performing invalid memory access. Invalid memory access is performed on each platform as follows:

Fatalerror

Crash when using fatal error in Unity. implementation.

Abort

Fail by calling the abort () function.

PureVirtualFunction

Cause a failure by calling a purely virtual function to raise an exception.


For older versions of Unity:

UnityEngine.Application.ForceCrash(int mode)

For older versions (Unity 5):

UnityEngine.Application.CommitSuicide(int mode)

Based on my experience, mode 0 causes a “unity processed” failure (where the Unity crash dialog box appears), and mode 2 causes a “complex” crash where a Windows error dialog appears.

This is similar to this Smilediver post on mode :

0 - simulates a failure, 1 - simulates a fatal error that Unity caught, 2 - will call abort ().

(These methods are not documented, as they are intended for internal use by Unity. They may also be labeled [Obsolete] depending on your version of Unity.)

0
source

Another option is to call

 System.Environment.FailFast("Error happened") 
0
source

Use the code below to close the application.

 Environment.Exit(1); 

To exit, a parameter named exitcode is required. If exitcode = 0 means there was no error. Set a nonzero exit code to reflect the error.

-2
source

Source: https://habr.com/ru/post/1445585/


All Articles