Simple program causes InvalidProgramException

G

Guest

Hello. When I run the program below, I get the exception
System.InvalidProgramException: Common Language Runtime detected an invalid
program. (Visual Studio 2005 version 8.0.50727.51)

I can work around it (by having the delegate point to a helper function).
Basically the purpose of this post is to let someone at Microsoft know about
this bug.

(The generated IL looks OK to me; my guess is that there's a problem in the
verifier.)


namespace Test {
public class Program {
public static void Main() {
Func("hello");
}

private delegate void FuncDelegate<T>(T item);

private static void Func<T>(T item) {
FuncDelegate<T> temp=new FuncDelegate<T>(Func);
}
}
}
 
G

Guest

Kosak,
Before you start reporting "bugs" , try doing this:

FuncDelegate<string> temp = new FuncDelegate<string>(Func);

You cannot use "T" as if it were an actual type in the implementation,
its just a placeholder for the actualy type you decide to use.
Hope that helps,
Peter
 
G

Guest

You are incorrect. If it helps you to appreciate my point, here is a similar
program that creates two delegates, the first one of which triggers the
runtime error and is guarded by a compilation directive.

You can easily control whether the program gets a runtime exception vs runs
successfully, by either commenting out the #define, or not. I hope this
suffices to disprove your claim about T being a placeholder.


#define MAKE_IT_FAIL
using System.Diagnostics;

namespace Test {
public class Program {
public static void Main() {
Func1("hello");
}

private delegate void FuncDelegate<T>(T item);

private static void Func1<T>(T item) {
#if MAKE_IT_FAIL
FuncDelegate<T> temp1=new FuncDelegate<T>(Func1);
#endif
FuncDelegate<T> temp2=new FuncDelegate<T>(Func2);
temp2(item);
}

private static void Func2<T>(T item) {
Debug.WriteLine(item);
}
}
}
 
J

Jon Skeet [C# MVP]

kosak said:
Hello. When I run the program below, I get the exception
System.InvalidProgramException: Common Language Runtime detected an invalid
program. (Visual Studio 2005 version 8.0.50727.51)

I can work around it (by having the delegate point to a helper function).
Basically the purpose of this post is to let someone at Microsoft know about
this bug.

(The generated IL looks OK to me; my guess is that there's a problem in the
verifier.)

Interesting - from the command line, which uses the compiler version
8.00.50727.42 - it all works fine for me.
 
B

Barry Kelly

kosak said:
If it helps you to appreciate my point, here is a similar
program that creates two delegates, the first one of which triggers the
runtime error and is guarded by a compilation directive.

This program you produce below causes the error for me (CLR 50727.42),
but not your first program.
#define MAKE_IT_FAIL
using System.Diagnostics;

namespace Test {
public class Program {
public static void Main() {
Func1("hello");
}

private delegate void FuncDelegate<T>(T item);

private static void Func1<T>(T item) {
#if MAKE_IT_FAIL
FuncDelegate<T> temp1=new FuncDelegate<T>(Func1);
#endif
FuncDelegate<T> temp2=new FuncDelegate<T>(Func2);
temp2(item);
}

private static void Func2<T>(T item) {
Debug.WriteLine(item);
}
}
}

Peverify fails for the generated assembly, but like you say, the IL
looks good to me. Looks like a JIT compiler bug - Mono executes the
assembly fine, BTW.

To log a bug, you should post it in the Connect site:

http://connect.microsoft.com/visualstudio/

-- Barry
 
G

Guest

Very interesting re the difference between the two programs. I will post
something to Connect as you suggest. Thank you, and also thanks to Jon
Skeet, for your feedback!

Kind regards,
Corey
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top