Bugzilla – Bug 322670
[PATCH (workaround)] gmcs crash when pass an generic anonymous method to a constructor
Last modified: 2007-09-15 21:24:23 UTC
---- Reported by redforks@gmail.com 2006-11-19 02:49:31 MST ---- Pass an generic anonymous method to a constructor, like this: delegate void TestFunc<T>(T val); class A { public A(TestFunc<int> func) { } } class TestClass { readonly A a = new A(delegate(int a) { }); static void Func<T>(TestFunc<T> func) { } } gmcs will crash: Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Mono.CSharp.AnonymousMethod.DoCreateMethodHost (Mono.CSharp.EmitContext ec) [0x00093] in /tmp/scratch/BUILD/mono-1.2/mcs/mcs/anonymous.cs:1363 at Mono.CSharp.AnonymousContainer.Resolve (Mono.CSharp.EmitContext ec) [0x0012a] in /tmp/scratch/BUILD/mono-1.2/mcs/mcs/anonymous.cs:1246 at Mono.CSharp.AnonymousMethod.Resolve (Mono.CSharp.EmitContext ec) [0x00000] in /tmp/scratch/BUILD/mono-1.2/mcs/mcs/anonymous.cs:1379 at Mono.CSharp.AnonymousMethodExpression.Compatible (Mono.CSharp.EmitContext ec, System.Type delegate_type) [0x00321] in /tmp/scratch/BUILD/mono-1.2/mcs/mcs/anonymous.cs:1111 at Mono.CSharp.Convert.ImplicitConversionStandard (Mono.CSharp.EmitContext ec, Mono.CSharp.Expression expr, System.Type target_type, Location loc) [0x001b2] in /tmp/scratch/BUILD/mono-1.2/mcs/mcs/convert.cs:1342 at Mono.CSharp.Convert.ImplicitConversion (Mono.CSharp.EmitContext ec, Mono.CSharp.Expression expr, System.Type target_type, Location loc) [0x00012] in /tmp/scratch/BUILD/mono-1.2/mcs/mcs/convert.cs:1233 at Mono.CSharp.Invocation.VerifyArgumentsCompat (Mono.CSharp.EmitContext ec, System.Collections.ArrayList Arguments, Int32 arg_count, System.Reflection.MethodBase method, Boolean chose_params_expanded, System.Type delegate_type, Boolean may_fail, Location loc) [0x000b1] in /tmp/scratch/BUILD/mono-1.2/mcs/mcs/expression.cs:4895 at Mono.CSharp.Invocation.OverloadResolve (Mono.CSharp.EmitContext ec, Mono.CSharp.MethodGroupExpr me, System.Collections.ArrayList Arguments, Boolean may_fail, Location loc) [0x00609] in /tmp/scratch/BUILD/mono-1.2/mcs/mcs/expression.cs:4802 at Mono.CSharp.New.DoResolve (Mono.CSharp.EmitContext ec) [0x0033c] in /tmp/scratch/BUILD/mono-1.2/mcs/mcs/expression.cs:5741 at Mono.CSharp.Expression.Resolve (Mono.CSharp.EmitContext ec, ResolveFlags flags) [0x00067] in /tmp/scratch/BUILD/mono-1.2/mcs/mcs/ecore.cs:415 at Mono.CSharp.Expression.Resolve (Mono.CSharp.EmitContext ec) [0x00000] in /tmp/scratch/BUILD/mono-1.2/mcs/mcs/ecore.cs:442 at Mono.CSharp.FieldBase.ResolveInitializer () [0x00022] in /tmp/scratch/BUILD/mono-1.2/mcs/mcs/class.cs:5613 at Mono.CSharp.ClassOrStruct.DefineFieldInitializers () [0x00025] in /tmp/scratch/BUILD/mono-1.2/mcs/mcs/class.cs:2793 at Mono.CSharp.ClassOrStruct.Define () [0x00000] in /tmp/scratch/BUILD/mono-1.2/mcs/mcs/class.cs:2834 at Mono.CSharp.RootContext.DefineTypes () [0x000e2] in /tmp/scratch/BUILD/mono-1.2/mcs/mcs/rootcontext.cs:585 at Mono.CSharp.Driver.MainDriver (System.String[] args) [0x0053e] in /tmp/scratch/BUILD/mono-1.2/mcs/mcs/driver.cs:1675 at Mono.CSharp.Driver.Main (System.String[] args) [0x0001f] in /tmp/scratch/BUILD/mono-1.2/mcs/mcs/driver.cs:292 And I made a patch: Index: anonymous.cs =================================================================== --- anonymous.cs (revision 68052) +++ anonymous.cs (working copy) @@ -1361,7 +1361,7 @@ member_name = new MemberName (name, args, Location); generic_method = new GenericMethod ( - RootScope.NamespaceEntry, RootScope, member_name, + RootScope != null ? RootScope.NamespaceEntry : null, RootScope, member_name, new TypeExpression (ReturnType, Location), Parameters); generic_method.SetParameterInfo (null); ---- Additional Comments From redforks@gmail.com 2006-11-19 02:50:36 MST ---- Created an attachment (id=170916) test csharp file ---- Additional Comments From redforks@gmail.com 2006-11-19 02:51:02 MST ---- Created an attachment (id=170917) The patch ---- Additional Comments From rharinath@novell.com 2006-11-22 07:08:39 MST ---- Hmm, the posted patch would paper over the actual bug, and cause other bugs elsewhere. So, I'm not applying it to the tree. Still, it's probably a useful workaround till we fix the bug -- in case anyone else is bitten by the same bug. ---- Additional Comments From redforks@gmail.com 2006-11-22 22:53:38 MST ---- I don't understand: RootScope.NamespaceEntry RootScope != null ? RootScope.NamespaceEntry : null Are there acture difference? You don't fix the bug, is just ok. Because I found gmcs/mcs is not very usefull, I use csc instead, and try to run under mono and linux. Two years, csc/gmcs can't build any of my csharp projects. I waited and waited, one bug fixed, many new bugs appeared. Maybe another 2 years needed for mcs/gmcs usable, or maybe not. Sorry my complain, I just can't wait any more. ---- Additional Comments From rharinath@novell.com 2006-11-23 09:24:39 MST ---- I'm talking about a development tree, not a release tree. It's better not to paper over bugs in the development tree, so that we can fix it the right way. In this particular case, 'RootScope' is null: either it shouldn't be, or we shouldn't try to use its version of NamespaceEntry -- deciding on either of these, and fixing the remaining code will require some thought and some rewriting/refactoring. (Passing a null NamespaceEntry is not right, since, somewhere else, the code _will_ dereference that NamespaceEntry, and it will be harder to debug the problem when it NullRefs there.) In any case, surrounding and related code are being rewritten, and we'll be in a better position to judge how to fix this bug once that is done. ---- Additional Comments From martin@ximian.com 2006-11-24 09:56:48 MST ---- Just use `Host.NamespaceEntry' there - RootScope / Scope may be null, but `Host' not. ---- Additional Comments From rharinath@novell.com 2006-12-01 07:30:06 MST ---- This appears to have been fixed as part of Martin's recent rewrite. Imported an attachment (id=170916) Imported an attachment (id=170917)