How to make C# application crash

2020-02-28 05:29发布

I want to test if My application crash dump can be debugged, so I need generate a crash dump of my application firstly, but I write it with C#, so anybody know how to trigger a crash.(In fact I test with many exceptions, unsafe code......, but don't get it). thanks ---- sorry, sorry,I just lost somethings: I build the application with unity3d, which will handle exceptions for me automatic, and generate a crash dump for me if application crash

thanks all for your answers, I just test all your methods in common c# application and all works, but not unity3d application written with C#, it seems unity3d do more, I think I need to email unity3d to get a answer. I will post here if I get it.

标签: c# unity3d crash
12条回答
我命由我不由天
2楼-- · 2020-02-28 06:01

StackOverflowException is a badass:

void PerformOverflow()
{
  PerformOverflow();
}

Usage:

PerformOverflow();
查看更多
Lonely孤独者°
3楼-- · 2020-02-28 06:01

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);
    }
查看更多
贪生不怕死
4楼-- · 2020-02-28 06:02

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);
}
查看更多
Evening l夕情丶
5楼-- · 2020-02-28 06:03

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.

查看更多
Lonely孤独者°
6楼-- · 2020-02-28 06:03
  public void Loop()
  {
      Loop();
  }
  //call this
  Loop();
查看更多
叼着烟拽天下
7楼-- · 2020-02-28 06:09

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

查看更多
登录 后发表回答