Stack Imbalance due to InterOp.

Posted by nhughes Tue, 03 May 2005 07:20:00 GMT

Don't get bitten by this invisible bug.

If your .NET application has an interop to managed code and the managed code calls back into your .NET code (using delegates), make sure you have the calling conventions of the delegate and the interop methods defined the same.

If you don’t do this, you will cause a stack imbalance, which may go unnoticed. Errors of this type seem to only be reported by Managed Debugging Assistant in Visual Studio 2005 Team Suite Beta 2 (not in VS 2005 Beta 2 standard). The error shows up as:

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

To resolve this, make sure the delegate calling convention matches the calling convention of the interop methods. For example, here is the definition of an interop method on a dll, whose calling convention is cdecl:

     [DllImport("Implode.dll", CallingConvention = CallingConvention.Cdecl)]
     public extern static uint implode(
          PKCompression.ReadMemoryEventHandler ReadMemDelegate,
          PKCompression.WriteMemoryEventHandler WriteMemDelegate,
          IntPtr work_buf,
          ref PKCompression.CompressionMemoryParameters Param,
          ref uint type, ref uint dsize);

The delegate that this implode.dll calls back into should also define this same cdecl calling convention. Like this:

     [UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
     internal delegate void WriteMemoryEventHandler(
          IntPtr buff, ref uint size, 
          ref CompressionMemoryParameters memoryParameters);

Comments are disabled