"Common Language Runtime detected an invalid program" – InvalidProgramException – When your application has a really large method.

If you've written a whole bunch (and I mean a really big WHOLE bunch) of code inside a method, constructor or the like, there is a good chance you'll see this exception.

We had an application that would generate an Assembly using CodeDom based on certain parameters. The parameter list for one scenario got so huge that the app generated a class with a constructor with 39,000 lines of code. When we tried instantiating an object of this class our app would just crash with a "Common Language Runtime has detected an invalid program" - InvalidProgramException.

PEVerify.exe would not return any errors (and say that the IL is just fine) but when you try and run NGEN.exe it would fail at that gigantic method.

So the problem is with the JIT compiler. There is a limitation on the number of local variable the JIT compiler in the CLR can handle for .NET 2.0 - which was restricted to a max of 32767 (which includes the local used by the JIT itself as well). So once in a blue moon with a GIGANTIC method that you just can't do with out for some strange reason will give you this Exception. This problem is documented in KB (I would like to point out that this is not the only scenario in which the CLR would throw the InvalidProgramException, this is one of the reasons you'd see it).

There is a Hotfix available for this problem now which increases the limit of local variables from 32767 to 4 million. The problem (and hence the reason for this blog entry) is that KB 919514 (http://support.microsoft.com/?kbid=919514), which is related to the Hotfix doesn't seem related at all from the description. And the KB that does sound related KB 312544 (http://support.microsoft.com/kb/312544 - problem  #3) doesn't speak of the Hotfix. Our appologies for this confusion - I've written an email to the content team to resolve this documentation issue.

Till then please read KB 919514 and contact PSS for the Hotfix. This fix has already been branched into the Orcas release.

 UPDATE (29/12/2006) : I just got a notification that the content team has updated KB 312544 to reflect the availability of the Hotfix.

Comments (13)

  1. oidon says:

    Increasing the limit is an easy fix. However, was it intentionally restricted to 32,767? I am not sure that I am comfortable having a few million local variables in a single method. What is the desired functionality?

  2. "I am not sure that I am comfortable having a few million local variables in a single method"

    [mjeelani] You are right – you shouldn’t be comfortable till you break that method into multiple methods and do an aggressive code review on it. However, from a framework perspective I think the 32767 limit was being hit very often especially by the COBOL developers hence I believe the limit was increased.

  3. Gaurav says:

    I have a method with just a few lines of code and there is a call to another method from there which never takes place!

    The exception is exactly what has been described.

  4. Mohammad Reza Jooyandeh says:

    I have a mixture of .NET 2.0 and VC++6.0 code. I wrote my coclass in VC and implement IList, ICollection and IEnumerable which are comvisible.

    Then I used the coclass in .NET because of that implementation I can use the object as an ICollection object. But whenever I call one its methods I caught an InvalidProgramException.

    How should I do?!

  5. Danie Bruwer says:

    I do support on a SharePoint Portal (2003, Framework 1.1). Last week after we deployed some new functionality

  6. Allen B. Taylor says:

    I got this exception in a C++/CLI app built in release mode (debug mode works fine). It happens when a native method is called from another native method. The call never happens. Disabling optimization on the called module has fixed the problem. Nasty.

  7. Ku Prabhu says:

    Thank you so much dudes.

    Now its working fine to me. After I break into many methods from one large method(39087 lines).

    Thanks a lot once again.

  8. Llyle says:

    I managed to get this exception while using Test Driven .Net with a RowTest (which is smallish). A simple solution recompile fixed this for me.

  9. Troy Howard says:

    @Allen B. Taylor – If you still want optimization turned on the for rest of the code, but just want to avoid this exception scenario, you can decorate the sensitive methods with [MethodImpl(MethodImplOptions.NoOptimization)] attribute and the compiler will skip optimizations for that method. I had to do that in a recent program that was doing some byte to struct mapping stuff using Marshal.StructureToPtr/PtrToStructure, etc.. Compiler couldn’t figure it out, and so, decorating those "low level" methods that moved the byte data around with that attribute allowed me to get a good build with optimization turned on for the rest of the code.

    Hope that helps,


  10. Gernot says:

    I only get this error in debugging mode if I've pause and changed a lot of code, and then F5. Sometimes a certain sub won't execute somewhere after the F5 with this error.

    It then keeps happening on that same sub until I Re-Build the project. I might throw a Clean Project in there too. never really stopped to right down the procedure i go through to clear it. Figured it must just be another bug. (using vs2010 SP1)

    Btw, not really going into a any BIG subs when I get this error. I often get compile errors on F5 that are really odd. Then when I Clean & Re-Build (from Build Menu) they all go away.

  11. Heider Sati says:

    Well, (for me), I managed to fix this problem on my side after an hour of investigations.

    In VB.NET (v4.0 VS-SP1), I found a bug where the compiler does throw an error neither does the IntelliSense:

    Notice below, if I add the following line in a function (anywhere in the program, Class / Module / Form / etc):

    MyGraphics.DrawImage(OriginalImage.Clone(New System.Drawing.Rectangle(ClipX1, ClipY1, ClipW, ClipH), Drawing.Imaging.PixelFormat.Format24bppRgb), 0, 0, TempWidth, TempHeight)

    The compiler throws this error before it even hits the function while debugging, it does not even reach the line (just adding to the functoin, causes the compiler to be scared of even entering this function).

    I copied the line from another function in my code, the existing function works fine, however the new one doesn't. Which is quite shocking to be honest.

    Now, I double-checked again and found the cause, as the "OriginalImage" object was defined as a "Bitmap" in the code that was working, however, I defined it as an "Image" in the one that did not work.

    I further went to the code line above, and checked the parameters / arguments that the "clone" function above takes, the one from the "Bitmap" instance has the parameters correct and with 3 morphs (i.e. 3 different set of parameters you can call the function with), however, with the "Image" type instance, the "Clone" takes no parameters !.

    I was quite shoekd to see this, the problem is that I didn't get any error nor warning in IntelliSense, nor compiler-error, the code compiled fine, but the function couldn't execute, I am sure this is a but (Ahem; … a Feature) in .NET.

    Fixng it was to change the "system.drawing.Image" into "system.drawing.Bitmap", code worked afterwards.

    I hope this helps

    Kind Regards


  12. Sandy says:

    @Heider Sati: That's why you should always enable Option Strict!

  13. Ramesh says:

    Hi guys i am getting the Error , could you please help me.

    And  I have taken latest code from tfs still im getting same error.

    If any one please send to my mail  rameshbabu1225@gmail.com


    System.InvalidProgramException: Common Language Runtime detected an invalid program.

Skip to main content