调试器中的AccessViolationException仅包含托管代码和WCF服务

时间:2011-03-03 15:42:55

标签: c# .net wcf access-violation

此应用程序具有由WinForms应用程序调用的WCF Web服务。 WinForms应用程序有一个类WCFCache来管理来自服务的数据。在该类中有一个这样的部分来管理机器子集具有的可选自定义配置部分:

private bool? m_HasCustomConfiguration = null;
public bool HasCustomConfiguration
{
    get
    {
        if (m_HasCustomConfiguration == null)
            m_HasCustomConfiguration = (CustomConfiguration != null);
        return (bool)m_HasCustomConfiguration;
    }
}

private WCFService.CustomConfiguration m_CustomConfiguration = null;
public WCFService.CustomConfiguration CustomConfiguration
{
    get
    {
        if (m_CustomConfiguration == null)
        {
            if (m_HasCustomConfiguration.HasValue
                && !m_HasCustomConfiguration.Value)
                return null;
            try
            {
                using (WCFService.WCFServiceClient wcf = new WCFService.WCFServiceClient())
                {
                    m_CustomConfiguration = 
                        wcf.GetCustomConfiguration(Machine.ProcessID);
                        // Above method returns null if no record exists.
                    m_HasCustomConfiguration = (m_CustomConfiguration != null);
                }
            } catch (Exception e) {
                // Error logging & re-throw
            }
        }
        return m_CustomConfiguration;
    }
}

当我在调用上述任何一个属性的代码中单步执行调试器时:

if (!Program.WCFCache.HasCustomConfiguration)
    return new List<CustomComponents>();

...它抛出以下异常:

System.AccessViolationException was unhandled
Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Source="System.Windows.Forms"
...

当我将步骤包含引用的行时,会有一个很长的暂停,然后是一个带有异常的VS弹出窗口。

当我在上面的代码执行完后放入断点时,不会出现异常。当我在属性的访问器中放置断点时,它甚至不会出现。它只发生在我从外面踩到其中一个属性的线上时。 (所以有一种解决方法,但这可能很痛苦。)

为什么会这样?我可以阻止吗?

编辑:整个应用程序是去年在3.5 C#/ .NET中编写的,WCF在组件之间进行通信;意思是,我们没有传统的非托管DLL。只有两个非托管调用:一个是在加载WinForms应用程序时使用advapi32.dll,在用户名检测过程中。我遇到的问题只发生在代码中的这一个地方,在一个与登录部分无关的地方。另一个是kernel32.dll,在GC强制冲洗长之后完成了上述调用的结果。

2 个答案:

答案 0 :(得分:2)

您使用的是任何P / Invoke或其他此类本机代码吗?赔率是,你应该开始寻找。

这个例外是一个更大问题的症状,即内存损坏(毕竟这个例外是什么)。如果这是一个本机应用程序,你会崩溃。

因此,查看任何本机调用,确保它们正常运行,可以在调试器下运行它们以尝试将错误捕获到离家更近的地方。

对不起,鉴于这种情况,无法给出更好的建议。

答案 1 :(得分:2)

我最终发现其他人遇到过这种情况并且很可能是Visual Studio错误。问题发生时我正在使用VS 2008。