Bug 8393 - ILGenerator.Emit - Null reference exception when dynamically declaring a class
Summary: ILGenerator.Emit - Null reference exception when dynamically declaring a class
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 4.4.x
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-11-14 07:20 UTC by Roman Alifanov
Modified: 2013-05-22 16:12 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Roman Alifanov 2012-11-14 07:20:53 UTC
I attempt to use the Evaluator class from Mono MCS in my Android application.

Everything works fine, except class declaration, evaluating following code causes System.Reflection.Emit.ILGenerator , Emit method to throw a NullReferenceException

It looks like an internal bug in mono's implementation of System.Reflection.Emit.ILGenerator , Emit method.

Steps to reproduce:

var c = new Evaluator();
c.Evaluate("public class x{ }");
c.Evaluate("var i = new x();");

Result:

{System.NullReferenceException: Object reference not set to an instance of an object
  at System.Reflection.Emit.ModuleBuilder.GetToken (System.Reflection.MemberInfo member, Boolean create_open_instance) [0x00000] in <filename unknown>:0 
  at System.Reflection.Emit.ModuleBuilderTokenGenerator.GetToken (System.Reflection.MemberInfo member, Boolean create_open_instance) [0x00000] in <filename unknown>:0 
  at System.Reflection.Emit.ILGenerator.Emit (OpCode opcode, System.Reflection.ConstructorInfo con) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.EmitContext.Emit (OpCode opcode, Mono.CSharp.MethodSpec method) [0x00033] in x:\src\Android\Addo.DocBox\Addovation.DocBox\Mono.CSharp\codegen.cs:415 
  at Mono.CSharp.New.Emit (Mono.CSharp.EmitContext ec, IMemoryLocation target) [0x00139] in x:\src\Android\Addo.DocBox\Addovation.DocBox\Mono.CSharp\expression.cs:5866 
  at Mono.CSharp.New.Emit (Mono.CSharp.EmitContext ec) [0x0002f] in x:\src\Android\Addo.DocBox\Addovation.DocBox\Mono.CSharp\expression.cs:5878 
  at Mono.CSharp.FieldExpr.EmitAssign (Mono.CSharp.EmitContext ec, Mono.CSharp.Expression source, Boolean leave_copy, Boolean isCompound) [0x0008f] in x:\src\Android\Addo.DocBox\Addovation.DocBox\Mono.CSharp\ecore.cs:5689 
  at Mono.CSharp.HoistedVariable.EmitAssign (Mono.CSharp.EmitContext ec, Mono.CSharp.Expression source, Boolean leave_copy, Boolean isCompound) [0x00001] in x:\src\Android\Addo.DocBox\Addovation.DocBox\Mono.CSharp\anonymous.cs:809 
  at Mono.CSharp.VariableReference.EmitAssign (Mono.CSharp.EmitContext ec, Mono.CSharp.Expression source, Boolean leave_copy, Boolean prepare_for_load) [0x00012] in x:\src\Android\Addo.DocBox\Addovation.DocBox\Mono.CSharp\expression.cs:4833 
  at Mono.CSharp.Assign.Emit (Mono.CSharp.EmitContext ec, Boolean is_statement) [0x0000d] in x:\src\Android\Addo.DocBox\Addovation.DocBox\Mono.CSharp\assign.cs:401 
  at Mono.CSharp.Assign.EmitStatement (Mono.CSharp.EmitContext ec) [0x00001] in x:\src\Android\Addo.DocBox\Addovation.DocBox\Mono.CSharp\assign.cs:411 
  at Mono.CSharp.BlockVariableDeclaration.DoEmit (Mono.CSharp.EmitContext ec) [0x0001b] in x:\src\Android\Addo.DocBox\Addovation.DocBox\Mono.CSharp\statement.cs:1639 
  at Mono.CSharp.Statement.Emit (Mono.CSharp.EmitContext ec) [0x0000e] in x:\src\Android\Addo.DocBox\Addovation.DocBox\Mono.CSharp\statement.cs:78 
  at Mono.CSharp.Block.DoEmit (Mono.CSharp.EmitContext ec) [0x00006] in x:\src\Android\Addo.DocBox\Addovation.DocBox\Mono.CSharp\statement.cs:2370 
  at Mono.CSharp.ExplicitBlock.Emit (Mono.CSharp.EmitContext ec) [0x0008a] in x:\src\Android\Addo.DocBox\Addovation.DocBox\Mono.CSharp\statement.cs:2523 
  at Mono.CSharp.ParametersBlock.Emit (Mono.CSharp.EmitContext ec) [0x0003d] in x:\src\Android\Addo.DocBox\Addovation.DocBox\Mono.CSharp\statement.cs:2960 
  at Mono.CSharp.ToplevelBlock.Emit (Mono.CSharp.EmitContext ec) [0x00052] in x:\src\Android\Addo.DocBox\Addovation.DocBox\Mono.CSharp\statement.cs:3514 
  at Mono.CSharp.MethodData.Emit (Mono.CSharp.TypeDefinition parent) [0x00095] in x:\src\Android\Addo.DocBox\Addovation.DocBox\Mono.CSharp\method.cs:2110 
  at Mono.CSharp.MethodOrOperator.Emit () [0x001ca] in x:\src\Android\Addo.DocBox\Addovation.DocBox\Mono.CSharp\method.cs:714 
  at Mono.CSharp.Method.Emit () [0x00165] in x:\src\Android\Addo.DocBox\Addovation.DocBox\Mono.CSharp\method.cs:1318 }
Comment 1 Craig Whiteside 2013-01-14 22:52:42 UTC
I would also like to confirm this bug, here is the offending code I am using, and it causes the same exact error.

public class NPC { public int i; }

However there is a workaround that I am currently using, by introducing a static within the class to return a new instance of itself will however work fine.

public class NPC { public int i; public static NPC New() { return new NPC(); } }
Comment 2 Jonathan Pryor 2013-05-22 16:12:15 UTC
Please try again on the 4.7.x alpha/beta release. That uses Mono 3.0 as the basis, which has lots of System.Reflection.Emit-related changes.