Share via


pInvokeStackImbalance MDA

The pInvokeStackImbalance managed debugging assistant (MDA) is activated when the CLR detects that the stack depth after a platform invoke call does not match the expected stack depth, given the calling convention specified in the DllImportAttribute attribute as well as the declaration of the parameters in the managed signature.

Note

The pInvokeStackImbalance MDA is implemented only for 32-bit x86 platforms.

Note

In the .NET Framework version 3.5, the pInvokeStackImbalance MDA is disabled by default. When you use the .NET Framework version 3.5 with Visual Studio 2005, the pInvokeStackImbalance MDA will appear in the Managed Debugging Assistants list in the Exceptions dialog box (which is displayed when you click Exceptions on the Debug menu). However, selecting or clearing the Thrown check box for pInvokeStackImbalance does not enable or disable the MDA; it only controls whether Visual Studio throws an exception when the MDA is activated.

Symptoms

An application encounters an access violation or memory corruption when making or following a platform invoke call.

Cause

The managed signature of the platform invoke call might not match the unmanaged signature of the method being called. This mismatch can be caused by the managed signature not declaring the correct number of parameters or not specifying the appropriate size for the parameters. The MDA can also activate because the calling convention, possibly specified by the DllImportAttribute attribute, does not match the unmanaged calling convention.

Resolution

Review the managed platform invoke signature and calling convention to confirm it matches the signature and calling convention of the native target. Try explicitly specifying the calling convention on both the managed and unmanaged sides. It is also possible, although not as likely, that the unmanaged function unbalanced the stack for some other reason, such as a bug in the unmanaged compiler.

Effect on the Runtime

Forces all platform invoke calls to take the nonoptimized path in the CLR.

Output

The MDA message gives the name of the platform invoke method call that is causing the stack imbalance. A sample message of a platform invoke call on method SampleMethod is:

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.

Configuration

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

See Also

Reference

MarshalAsAttribute

Concepts

Diagnosing Errors with Managed Debugging Assistants

Other Resources

Interop Marshaling

Interoperability