4

我相信不可能在 .Net 中的“纯”托管代码中产生/生成访问冲突异常。如果有人认为 .Net 完美无缺,并且不使用任何外部库(非托管),例如互操作。

我生活在幻想中吗?

4

4 回答 4

5
throw new AccessViolationException();

这是纯托管代码,它会产生 AccessViolationException :P

于 2013-01-17T19:19:06.827 回答
5

您还可以使用以下代码(尽管由于输入格式错误,它只会引发 AccessViolationException):

IntPtr ptr = new IntPtr(123);
Marshal.StructureToPtr(123, ptr, true);
于 2013-01-17T20:04:07.047 回答
4

You can e.g. use WPF which does call into your graphics card driver. You can easily get AcessViolationExceptions pre .NET 4.5 those with a buggy graphics card drivers which are not at all uncommon.

In a strange sense you are right. With .NET 4.5 and above you will never get AccessViolationExceptions in managed code anymore because the .NET runtime does not convert an AccessViolation coming from unmanaged code to an AccessViolationException anymore but it does terminate your process immediately. I guess MS support was tired to search for .NET Framework bugs only to find that it was a buggy graphics card driver.

于 2013-01-17T19:28:29.317 回答
2

您几乎永远不会看到 CPU 实际上异步抛出一个(在某事的中间),因为如果 'this' 在方法调用中为空,.NET 即时编译器通常会引发异常。在它可能使用 0 作为地址之前,它cmp [rcx],rcx会在调用站点引发异常。可以有足够大的字段偏移来读取带有空指针的可读内存,因此可以防止这种情况发生。

请参阅http://blogs.msdn.com/b/oldnewthing/archive/2007/08/16/4407029.aspx

没有魔法,C# 就像任何其他编译语言一样变成指令。没有理由对 AV 永远不会发生感到舒适。

于 2013-01-17T19:29:21.303 回答