我正在使用 Visual Studio 2010,并使用 C# 进行编码.我有一个在我的项目中使用的第三方 dll.当我尝试使用特定方法时,在看似随机的情况下,程序只是崩溃了,没有抛出异常.会话简单地结束.有什么方法可以追踪发生的事情吗?
I am using Visual Studio 2010, and coding in C#. I have a third-party dll that I am using in my project. When I attempt to use a specific method, at seemingly random occasions, the program simply crashes, with no exception thrown. The session simply ends. Is there any way I can trace what is going on?
Windows 中线程堆栈的布局方式是这样的(粗略;这不是对所发生的一切的准确描述,仅足以给你要点.CLR 处理堆栈页面的方式与非托管代码处理它的方式有些不同.)
The way the stack for a thread is laid out in Windows goes like this (roughly; this is not an exact description of everything that goes on, just enough to give you the gist. And the way the CLR handles stack pages is somewhat different than how unmanaged code handles it also.)
在堆栈的顶部是您正在使用的所有已提交页面.然后是一个保护页面"——如果你点击了那个页面,那么保护页面就会成为堆栈的新页面,而下一个页面就会成为新的保护页面.然而,栈的最后页是特殊的.如果你点击它一次,你会得到一个堆栈溢出异常.如果您点击它两次,那么进程将立即终止.我所说的立即"是指立即"——没有例外,直接进监狱,不要过关,不要收取 200 美元.操作系统的原因是,此时该进程已病入膏肓,并且可能对用户变得积极敌对.堆栈已溢出,溢出堆栈的代码可能正试图将任意多的垃圾写入内存.(*)
At the top of the stack there are all the committed pages that you are using. Then there is a "guard page" - if you hit that page then the guard page becomes a new page of stack, and the following page becomes the new guard page. However, the last page of stack is special. If you hit it once, you get a stack overflow exception. If you hit it twice then the process is terminated immediately. By "immediately" I mean "immediately" - no exception, go straight to jail, do not pass go, do not collect $200. The operating system reasons that at this point the process is deeply diseased and possibly it has become actively hostile to the user. The stack has overflowed and the code that is overflowing the stack might be trying to write arbitrarily much garbage into memory. (*)
由于该进程可能对其自身和其他人造成危害,因此操作系统会在不允许任何更多代码运行的情况下将其关闭.
Since the process is potentially a hazard to itself and others, the operating system takes it down without allowing any more code to run.
我怀疑您的非托管代码中的某些内容会两次访问最终堆栈页面.几乎每次我看到一个进程突然消失,无一例外或其他解释都是因为不要惹我"堆栈页面被点击.
My suspicion is that something in your unmanaged code is hitting the final stack page twice. Almost every time I see a process suddenly disappear with no exception or other explanation its because the "don't mess with me" stack page was hit.
(*) 早在 1990 年代初期,我就为一个名为 NetWare 的小型操作系统开发数据库驱动程序.它没有更现代的操作系统现在通常具有的这类保护.我需要能够在内核保护级别运行时动态切换堆栈";我知道我的驱动程序何时意外破坏了堆栈,因为它最终会写入屏幕内存,然后我可以通过查看直接写入屏幕的垃圾来调试问题.啊,那是那些日子.
(*) Back in the early 1990s I worked on database drivers for a little operating system called NetWare. It did not have these sorts of protections that more modern operating systems now have routinely. I needed to be able to "switch stacks" dynamically while running at kernel protection level; I knew when my driver had accidentally blown the stack because it would eventually write into screen memory and I could then debug the problem by looking at what garbage had been written directly to the screen. Ah, those were the days.
这篇关于第三方 dll 使程序崩溃,没有抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!