gpt4 book ai didi

c# - 只有托管代码和 WCF 服务的调试器中的 AccessViolationException

转载 作者:行者123 更新时间:2023-11-30 21:16:28 24 4
gpt4 key购买 nike

此应用程序有一个由 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 force-flush long after 上面的调用结果完成后。

最佳答案

您是否使用任何 P/Invoke 或其他此类 native 代码?很有可能,这就是您应该开始寻找的地方。

这个异常是一个更大问题的症状,即内存损坏(毕竟是异常所说的)。如果这是 native 应用程序,您会遇到崩溃。

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

抱歉,在这种情况下真的不能给出更好的建议。

关于c# - 只有托管代码和 WCF 服务的调试器中的 AccessViolationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5182992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com