Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4076066
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:22:41+00:00 2026-05-20T17:22:41+00:00

In VS2010, the managed debugging assistant will give you a pInvokeStackImbalance exception ( pInvokeStackImbalance

  • 0

In VS2010, the managed debugging assistant will give you a pInvokeStackImbalance exception (pInvokeStackImbalance MDA) if you call a function using the wrong calling convention, commonly because you didn’t specify CallingConvention = Cdecl when calling a C library. E.g. you wrote

[DllImport("some_c_lib.dll")]
static extern void my_c_function(int arg1, int arg2);

instead of

[DllImport("some_c_lib.dll", CallingConvention=CallingConvention.Cdecl)]
static extern void my_c_function(int arg1, int arg2);

and thus got the StdCall calling convention instead of Cdelc.

If you answer this, you already know the difference, but for other visitors to this thread: StdCall means that the callee cleans the arguments from the stack, whereas Cdecl means that the caller cleans up the stack.

So, if you get the calling convention wrong in your C code, your stack doesn’t get cleaned up and your program crashes.

However, .NET programs don’t seem to crash even though they use StdCall for Cdecl functions. The stack imbalance check wasn’t enabled by default on VS2008, so some VS2008 projects use the wrong calling convention unbeknownst to their authors. I just tried GnuMpDotNet and the sample runs just fine even though the Cdelc declaration is missing. The same holds true for X-MPIR.

They both throw the pInvokeStackImbalance MDA exception in debug mode, but don’t crash in release mode. Why is this? Does the .NET VM wrap all calls to native code and restore the stack itself afterwards? If so, why bother with the CallingConvention property at all?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:22:41+00:00Added an answer on May 20, 2026 at 5:22 pm

    It is because of the way the stack pointer is restored when the method exits. The standard prologue of a method, shown for the x86 jitter;

    00000000  push        ebp                 ; save old base pointer
    00000001  mov         ebp,esp             ; setup base pointer to point to activation frame
    00000003  sub         esp,34h             ; reserve space for local variables
    

    And the way it ends:

    0000014a  mov         esp,ebp             ; restore stack pointer
    0000014c  pop         ebp                 ; restore base pointer
    0000014d  ret 
    

    Getting the esp value unbalanced is not a problem here, it gets restored from the ebp register value. However, the jitter optimizer not infrequently optimizes this away when it can store local variables in cpu registers. You’ll crash and burn when the RET instruction retrieves the wrong return address from the stack. Hopefully anyway, really nasty when it happens to land on a chunk of machine code by accident.

    This is liable to happen when you run the release build without a debugger, tough to troubleshoot if you didn’t have the MDA to help you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the VS2010 debugger for managed code is it possible to trigger the finalizer
I have created a package in VS2010 RC using the MPF (Managed Package Framework)
Is it possible to build a managed c++ project in .net 3.5 using vs2010
Im getting a PInvokeStackImbalance error when running debug in VS2010 using selenium webdriver 2.5.1
I am writing a managed wrapper in C++/CLI (VS2010) for a third-party unmanaged library.
...using vs2010 and automating office 2007 The merge works fine but the saved document(pathToDestinationFile)
I have got a VS2010 solution managed by an instance of TFS2010. My solution
Using a VS2010 SP1 Data-tier application project, I created the simplest database I could:
About to start a new project in VS2010 which will have a few windows
I am using vs2010 and I want to access Nuget package management console.In project

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.