11

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 with many exceptions and unsafe code etc. but don't get it.

Thanks!

Edit: Sorry, Just forgot something, I'm making the application with Unity3D, which handles exceptions for me automatically.

Edit 2: Thanks all for your answers. I've tested your suggestions in a standard C# application and it all works fine, but not in my Unity3D application (written with C#). It seems like Unity3D requires more effort to cause a crash, I might email Unity3D to get a answer. I will post here if I get it. Cheers!

Suda
  • 234
  • 1
  • 4
  • 20
fieldChao
  • 176
  • 1
  • 1
  • 14

14 Answers14

23

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

System.Diagnostics.Debugger.Launch()
Giannis Paraskevopoulos
  • 18,261
  • 1
  • 49
  • 69
17

StackOverflowException is a badass:

void PerformOverflow()
{
  PerformOverflow();
}

Usage:

PerformOverflow();
Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89
11

Throw an exception :)

throw new Exception("Your exception here!");
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
sickUnit
  • 319
  • 2
  • 5
11

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.)

sonnyb
  • 3,194
  • 2
  • 32
  • 42
8

Well. The only good 100% way actualy crash CLR is to inject a native exception into the managed world.

Calling the Kernel32.dll's RaiseException() directly will immediately crash ANY C# application, and Unity Editor as well.

[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 crashing. Please note that in order to debug native and managed, you will need two instances of Visual Studio running. If you are developing native P/INVOKE plugin, set up it that Visual Studio Instance 1 is native debugger and uses Unity or your C# program as a Host program, and you attach to the Host program from another Visual Studio Instance.

Петър Петров
  • 1,966
  • 1
  • 17
  • 14
  • you do not need to SCREAM any time you want to highlight stuff. Use italic or bold works better... Just saying because it is annoying to see you scream even in comments... – WarrenFaith Aug 23 '16 at 07:51
  • 2
    It's an old habit like in when you always work with legalese to highlight stuff and important sentences, you use caps-lock... – Петър Петров Aug 30 '16 at 14:30
  • 1
    Thanks, this worked perfectly for me. The code generates a Unity-managed application crash. – Andreas Grech Aug 13 '17 at 22:29
7

Another option is to call

System.Environment.FailFast("Error happened")
max630
  • 8,762
  • 3
  • 30
  • 55
  • Worked and has the benefit of being the intended method provided by system library. – Tomáš Zato Feb 20 '20 at 15:51
  • Worked for testing crashing an ASP.NET Core application as well (most of the higher rated options didn't seem to do the trick) - thanks! – olucafont6 Sep 28 '20 at 20:27
  • 1
    This will put exception details in the Event Log, possibly being reported to Microsoft via Windows Error Reporting (WER). Maybe desired, maybe not. – l33t Dec 08 '21 at 08:56
6

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).

Mark Sowul
  • 10,244
  • 1
  • 45
  • 51
  • It seems unity3d application ignore the exception directly(not handle it like others), and has no any feedback. – fieldChao Aug 03 '13 at 04:12
  • If you want even less code... ThreadPool.QueueUserWorkItem(ignored => throw new Exception()); – TravisWhidden Jan 03 '19 at 23:47
  • 1
    Even less `ThreadPool.QueueUserWorkItem(_ => throw new Exception());` – Vlad Aug 14 '19 at 18:36
  • Note that Unity3d will log this exception as long as `UnityEngineApplication.logMessageReceivedThreaded` is used (rather than `logMessageReceived`) – sonnyb Aug 16 '19 at 16:26
0

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);
    }
Luzius
  • 164
  • 2
  • 10
0
  public void Loop()
  {
      Loop();
  }
  //call this
  Loop();
LuckyMN Z
  • 104
  • 6
0

I think there was a code in earlier unity versions like

Application.commitSuicide(number Input); 

Now it is replaced by

Application.ForceCrash(number input); 

Till this point, I dont know what different numbers do in number input, but for me,

Application.ForceCrash(1); 

does the job.

juagicre
  • 1,065
  • 30
  • 42
PMT_CODER
  • 1
  • 1
0

you could also divide by zero,

z = 0; 
int divide = 1 / x;
Mana
  • 1,925
  • 6
  • 39
  • 55
-1
 int[] x = {0};
 int blah = x[2];

will cause an exception just as well

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
-1

It's easy enough to reproduce if you try to transform a null game object. For example, like this:

public static GameObject gameObjectCrash;
public void GenerateCrash()
{
    gameObjectCrash.transform.rotation = Quaternion.Euler(90, 0, 0);
}
Dmytro Turkov
  • 880
  • 9
  • 12
-3

Use below code to close the application.

Environment.Exit(1);    

Exit needs a parameter called exitcode. If exitcode=0 means there was no error. Supply a non-zero exit code to to reflect an error.

Ashish Sahu
  • 185
  • 1
  • 6