How to debug corruption in the managed heap

后端 未结 2 1714
星月不相逢
星月不相逢 2020-12-07 12:11

My program throws an error which it cannot handle by a catch(Exception e) block and then it crashes:

Access Violation Corrupted State Exc

2条回答
  •  佛祖请我去吃肉
    2020-12-07 13:06

    You have managed heap corruption. It is not easy to find the root cause of the problem for managed heap corruption, because the problem usually demonstrates itself long after the heap is corrupted. In your case, the StringBuilder is a red herring. Corruption happened sometime before.

    What I would do is the following:

    1. Check if you have any unsafe C# code. If you have any, double check the logic there.
    2. Enable paged heap for your application. Running it with paged heap will help uncover problems with unmanaged code -- in case unmanaged code is corrupting the managed heap.
    3. Run !VerifyHeap in different places. This way you might be able to localize the place in your code where corruption happens.
    4. If you have the server type of garbage collection enabled for your application, temporarily change that to workstation garbage collection -- you will get more predictable behavior this way.
    5. Read through Tesses' blog post .NET Crash: Managed Heap Corruption calling unmanaged code. It demonstrates some examples of managed heap corruption.

    Note that when you will be running your code under WinDbg, you will come across occasional first chance AV. It is safe to ingore that, just type sxd av once you attach WinDbg to the process, and investigate only second chance AVs.

提交回复
热议问题