How to Disable a Specific MDA

As Mike Stall has noted in his post, the PInvokeStackImbalance MDA can be very slow, depending on the number of pinvoke calls in an application.  Recently, we have had a few customers running into this issue, but MSDN does not give much information regarding disabling MDAs except for setting COMPLUS_MDA to 0 or setting the registry key to 0.  The problem with these options is that they disable all MDAs.  Moreover, if you set the registry key, it has machine-wide impact.


There is actually a more fine-grained approach to do this.  You can actually disable an MDA using an MDA configuration file.  However, as mentioned on MSDN and in Stephen Toub’s article, the CLR will not look for the MDA configuration file unless either the environment variable or the registry key is set 1.  Doing so has no effect if an application has no MDA configuration file.


For example, to disable the PInvokeStackImbalance MDA, you can do this:


1)      Set the COMPLUS_MDA environment variable to 1 (or equivalently, set HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\MDA to “1”)


2)      Assuming your application is called hello.exe, create hello.exe.mda.config in the same directory and put the following XML in it:


<?xml version="1.0" encoding="UTF-8" ?>




    <pInvokeStackImbalance enable="false"/>





Note that MDA configuration files have precedence over MDAs activated by default for the managed debugger and the unmanaged debugger.  This means that an MDA configuration file can disable an MDA which is enabled automatically because a debugger is attached.  (In fact, it is the only way.)


Due to an oversight, the PInvokeStackImbalance MDA is not even defined on 64-bit (both X64 and IA64) platforms.  So if you try to use the MDA configuration file above on 64-bit platforms, you will get an error.  But then again, since we only have one calling convention on X64 and one on IA64, why would you need this MDA on 64-bit platforms anyway?  ;)