pInvokeStackImbalance MDA

如果 CLR 检测到平台 invoke 调用之后的堆栈深度与 DllImportAttribute 特性指定的调用约定中以及托管签名的参数声明中提供的预期堆栈深度不匹配,则将激活 pInvokeStackImbalance 托管调试助手 (MDA)。

注意注意

pInvokeStackImbalance MDA 仅对 32 位 x86 平台实现。

注意注意

在 .NET Framework 3.5 版本中,默认情况下禁用 pInvokeStackImbalance MDA。如果将 .NET Framework 3.5 版与 Visual Studio 2005 搭配使用,则 pInvokeStackImbalance MDA 将出现在“异常”对话框(单击“调试”菜单上的“异常”时会显示该对话框)中的“托管调试助手”列表中。但是,对 pInvokeStackImbalance 选中或清除“引发”复选框不会启用或禁用 MDA;只会控制激活 MDA 时 Visual Studio 是否会引发异常。

症状

在发出或跟随一个平台调用时,应用程序遇到访问冲突或内存损坏。

原因

平台调用的托管签名可能与要调用的方法的非托管签名不匹配。 这种不匹配情况可能是由以下原因造成的:托管签名声明的参数数量不正确或指定的参数大小不合适。 调用约定(可能由 DllImportAttribute 特性指定)与非托管调用约定不匹配,也会导致激活 MDA。

解决方法

检查托管平台调用签名和调用约定,以确认其与本机目标的签名和调用约定相匹配。 尝试在托管端和非托管端都显示指定调用约定。 虽然可能性不大,但也有可能是非托管函数由于其他某种原因(如非托管编译器中的缺陷)使堆栈失去了平衡。

对运行时的影响

强制所有平台调用都采用 CLR 中的非优化路径。

Output

MDA 消息中会给出导致堆栈失去平衡的平台调用方法调用的名称。 对 SampleMethod 方法发出的平台调用的示例消息是:

A call to PInvoke function 'SampleMethod' has unbalanced the stack. 
This is likely because the managed PInvoke signature does not match 
the unmanaged target signature. Check that the calling convention and 
parameters of the PInvoke signature match the target unmanaged signature.

配置

<mdaConfig>
  <assistants>
    <pInvokeStackImbalance />
  </assistants>
</mdaConfig>

请参见

参考

MarshalAsAttribute

概念

使用托管调试助手诊断错误

互操作封送处理

其他资源

互操作性