locked
A question related to "Common Language Runtime detected an invalid program"

    Question

  • Calling the following method will generate this error:

    private async void MethodA()
    {
        arrayFoo[i] = await fooDecocder.GetFoo();
    }

    However, calling the following method is OK: 

    private async void MethodB()
    {
        Foo foo = await fooDecocder.GetFoo();
        arrayFoo[i] = foo;
    }
    

    What is the difference between these two methods that is responsible for error  "Common Language Runtime detected an invalid program"?

    Class Foo is a simple structure. GetFoo() is a simple method created for debugging this:

    internal async Task<Foo> GetFoo()
    {
       Foo foo = new Foo();
       return foo;
    }


    Hong

    Sunday, April 13, 2014 7:45 PM

Answers

All replies

  • Sorry, but I probably oversimplified the description of the methods in order to make the problem clear. Here are the actual ones that I have been tested.

    Here is the method that causes the error:

    private async void MethodB()
    {
        Foo[] arrayFoo = new Foo[10]
        for(uint ui = 0; ui < 10; ui++)
        {
           arrayFoo[ui] = await fooDecocder.GetFoo();
        }
    }

    Here is the method that works fine:

    private async void MethodB()
    {
        Foo[] arrayFoo = new Foo[10]
        for(uint ui = 0; ui < 10; ui++)
        {
           Foo foo = await fooDecocder.GetFoo();
           arrayFoo[ui] = foo;   
        }
    }

    Here is what I have just found.  If I change the loop variable type from uint to int in MethodA, everything will work fine:

    private async void MethodB()
    {
        Foo[] arrayFoo = new Foo[10]
        for(int i = 0; i < 10; i++)
        {
           arrayFoo[i] = await fooDecocder.GetFoo();
        }
    }
    Why does uint cause the error in this case?

     


    Hong

    Sunday, April 13, 2014 11:02 PM
  • Can you put a sample project together with the exact code that causes a problem, then share a link to it?

    Both examples aren't compile-ready. 


    Darin R.

    Sunday, April 13, 2014 11:39 PM
  • I will try.  Please give me some time.  The code is exactly what I used to test, but it is a part of fairly complicated app.  I have the feeling this may be circumstantial (i.e. it happens only in a certain environment of a project).  I will try to make a bare-bone project.

    Hong

    Sunday, April 13, 2014 11:48 PM
  • I am happy to report that I have successfully reproduced this with a newly created bare-bone simple app.  MainPage.xaml.cs shows everything.

    What a relief!  I was afraid that this would happen only in a complex environment.  


    Hong

    Tuesday, April 15, 2014 1:23 PM
  • I ran the sample and see the crash too.

    I found that if I change this:

    arrayFoo[i] = await fooDecoder.GetFoo();

    to this:

    arrayFoo[(int)i] = await fooDecoder.GetFoo();

    ... it then also works (line 75). 

    It smells like a bit of a compiler bug to me...  uint should be a fine parameter to pass into an array like that.

     

    Darin R.

    Tuesday, April 15, 2014 1:38 PM
  • Thank you so much for looking into this, Darin.  It is greatly appreciated.

    I have had other issues since I installed VS2013 Update 2.  Are you using this update?

    What I fear most is that this is machine dependent.  What if it works on my development machine (Intel CPU), but not on other machines using Atom, ARM, etc.?  I am not afraid of compiling errors at all no matter how weird they are, but fear run-time errors.


    Hong

    Tuesday, April 15, 2014 1:47 PM
  • I'd file a bug report with visual studio itself on this one.  If you scan for any CLR runtime detected an invalid program errors, most all the support responses are to file a bug.

    I'm also running VS2013 Professional, Update 2 RC, windows 8.1 fully patched surface pro 2, fwiw.


    Darin R.

    Tuesday, April 15, 2014 2:37 PM
  • Thanks, Darin. I will.

    I hope someone running VS13 prior to Update 2 will test this to find out whether only Update 2 has this problem.


    Hong

    Tuesday, April 15, 2014 2:54 PM