奇怪的访问冲突异常 [英] Weird Access Violation Exception

查看:288
本文介绍了奇怪的访问冲突异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑与AccessViolationException的次数。这是相当IM posssible(请参阅回答)有一个干净的再现,但在这里不用的总体思路:

I'm puzzled with an occurance of AccessViolationException. It's quite imposssible (see answer) to have a clean reproduction but here goes the general idea:

class MyClass 
{
  public List<SomeType> MyMethod(List<string> arg)
  {
     // BREAKPOINT here
     // Simple stuff here, nothing fancy, no external libs used
  }
}

delegate List<SomeType> MyDelegate(List<string> arg);

... 

var myObject = new MyClass();

Func<List<string>, List<SomeType>> myFunc = myObject.MyMethod;
MyDelegate myDelegate = myObject.MyMethod;

myFunc(null)            // works fine
myDelegate(null)        // works fine
myObject.MyMethod(null) // throws AccessViolationException

怪异的部分是我没有使用任何不安全code。我没有对外部库望其项背(和在整个程序执行AFAIK的任何地方)。

The weird part is I'm not using any unsafe code. I don't have any dependencies on external libraries anywhere close (and anywhere in the whole program execution AFAIK).

最奇怪的部分是,这是100%可再现,甚至重构code略有下降,其他地区移动方法调用后,把多余的code才等 - 在所有情况下AccessViolationException仍然扔在那个特定的方法调用。该方法是从未进入时直接调用(断点未命中)。通过委托或函数功能:LT调用它。>正常工作

The weirdest part is this is 100% reproducable and even after refactoring the code slightly, moving the method invocation elsewhere, putting extra code before it etc. - in all cases AccessViolationException is still thrown on that particular method invocation. The method is never entered when invoked directly (breakpoint is not hit). Invoking it through delegate or Func<> works fine.

任何线索,这可能会导致它或如何调试呢?

Any clues as to what could cause it or how to debug it?

更新

以下antiduh的问题:有没有从构造望其项背调用一个虚方法。当这种情况发生的实际堆栈跟踪很简单,只有两个静态方法和一个简单的实例之一。

Following antiduh's question: there is no call to a virtual method from a constructor anywhere close. The actual stack trace when this happens is very simple, just two static methods and a simple instance one.

唯一的线索似乎是线程。有Parallel.ForEach()和Thread.sleep()方法调用的的这个程序执行(而不是在调用堆栈)。任何线索,如何把握不好线程(定期,管理类),可能会导致AVE?

The only clue seems to be threading. There is Parallel.ForEach() and Thread.Sleep() invoked before this in program execution (not in call stack). Any clues as to how mishandled threading (with regular, managed classes) could cause AVE?

更新

将范围缩小到一个VS错误,请参阅我的答案。

Narrowed it down to a VS bug, see my answer.

推荐答案

这似乎是一个 VS错误。看一看这个完整的解决方案。 code是简单的:

This seems to be a VS bug. Have a look at this full solution. Code is simple:

using System;
using System.Collections.Generic;

namespace Foo
{
    public class MyClass
    {
        public virtual object Foo(object o1, object o2, object o3, object o4)
        {
            return new object();
        }
    }

    public sealed class Program
    {
        public static void Main(string[] args)
        {
            var myClass = new MyClass();
            object x = new object();
            myClass.Foo(null, null, null, new object()); // put a breakpoint here and once it stops, step over (F10) - AccessViolationException should be thrown in VS
        }
    }
}

我之前已经错过了一个重要的事实是,code其实正常工作时正常运行。只有在那个特定的线路正在加紧了在VS(F10),访问冲突发生时,它实际上发生在VS宿主进程(尽管最后的堆栈帧是我的code)。这是可能的继续执行就好了。

The important fact I have missed before is that the code actually works fine when ran normally. Only when that particular line is being stepped over in VS (F10), the Access Violation occurs and it actually occurs in VS hosting process (even though the final stack frame is my code). It's possible to continue execution fine.

对我来说,问题发生在VS 2013,版本12.0.21005.1 REL。它也发生在3其他机器我测试的。

The issue happens for me on VS 2013, version 12.0.21005.1 REL. It also happens on 3 other machines I tested this on.

更新

安装的.NET Framework 4.5.2 解决了这一点。

这篇关于奇怪的访问冲突异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆