Trying to map an Ilist of Interfaces to an IList of implementations I am getting a Nullreference error. However I only receive this on iOS the same implementation works fine on Android. The error does not occur at the mapping the mapping goes through fine and the resulting collection appears to be fine. However after the event (onappearing) in which the map occurs completes only then does this error get raised. Presumably this is a Async method that doesnt complete till later a dispose or some other threading issue.
Downgrading to Automapper 4.2.1 resolves the error.
Good Luck - if this is half as hard to fix as it was to track down its going to be a pain for someone.
** Version
Using AutoMapper" version="5.1.1" targetFramework="portable45-net45+win8+wpa81" on iOS
** Error
System.NullReferenceException: Object reference not set to an instance of an object
** Code
var items = DataMapper.Mapper.Map
** Stack Trace
at Microsoft.Scripting.Interpreter.LoopCompiler.VisitVariable (System.Linq.Expressions.ParameterExpression node, Microsoft.Scripting.Ast.ExpressionAccess access) [0x00000] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LoopCompiler.cs:279
at Microsoft.Scripting.Interpreter.LoopCompiler.VisitParameter (System.Linq.Expressions.ParameterExpression node) [0x00000] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LoopCompiler.cs:271
at System.Linq.Expressions.ParameterExpression.Accept (System.Linq.Expressions.ExpressionVisitor visitor) [0x00000] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Ast/ParameterExpression.cs:118
at System.Linq.Expressions.ExpressionVisitor.Visit (System.Linq.Expressions.Expression node) [0x00006] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/System.Core/Microsoft/Scripting/Ast/ExpressionVisitor.cs:57
at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall (System.Linq.Expressions.MethodCallExpression node) [0x00000] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/System.Core/Microsoft/Scripting/Ast/ExpressionVisitor.cs:414
at System.Linq.Expressions.MethodCallExpression.Accept (System.Linq.Expressions.ExpressionVisitor visitor) [0x00000] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Ast/MethodCallExpression.cs:106
at System.Linq.Expressions.ExpressionVisitor.Visit (System.Linq.Expressions.Expression node) [0x00006] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/System.Core/Microsoft/Scripting/Ast/ExpressionVisitor.cs:57
at System.Linq.Expressions.ExpressionVisitor.VisitBinary (System.Linq.Expressions.BinaryExpression node) [0x00000] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/System.Core/Microsoft/Scripting/Ast/ExpressionVisitor.cs:198
at Microsoft.Scripting.Interpreter.LoopCompiler.VisitBinary (System.Linq.Expressions.BinaryExpression node) [0x000b4] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LoopCompiler.cs:254
at System.Linq.Expressions.BinaryExpression.Accept (System.Linq.Expressions.ExpressionVisitor visitor) [0x00000] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Ast/BinaryExpression.cs:331
at System.Linq.Expressions.ExpressionVisitor.Visit (System.Linq.Expressions.Expression node) [0x00006] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/System.Core/Microsoft/Scripting/Ast/ExpressionVisitor.cs:57
at System.Linq.Expressions.ExpressionVisitor.VisitConditional (System.Linq.Expressions.ConditionalExpression node) [0x00000] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/System.Core/Microsoft/Scripting/Ast/ExpressionVisitor.cs:250
at System.Linq.Expressions.ConditionalExpression.Accept (System.Linq.Expressions.ExpressionVisitor visitor) [0x00000] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Ast/ConditionalExpression.cs:93
at System.Linq.Expressions.ExpressionVisitor.Visit (System.Linq.Expressions.Expression node) [0x00006] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/System.Core/Microsoft/Scripting/Ast/ExpressionVisitor.cs:57
at System.Linq.Expressions.ExpressionVisitor.VisitLoop (System.Linq.Expressions.LoopExpression node) [0x00000] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/System.Core/Microsoft/Scripting/Ast/ExpressionVisitor.cs:378
at System.Linq.Expressions.LoopExpression.Accept (System.Linq.Expressions.ExpressionVisitor visitor) [0x00000] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Ast/LoopExpression.cs:82
at System.Linq.Expressions.ExpressionVisitor.Visit (System.Linq.Expressions.Expression node) [0x00006] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/System.Core/Microsoft/Scripting/Ast/ExpressionVisitor.cs:57
at Microsoft.Scripting.Interpreter.LoopCompiler.CreateDelegate () [0x00000] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LoopCompiler.cs:82
at Microsoft.Scripting.Interpreter.EnterLoopInstruction.Compile (System.Object frameObj) [0x00074] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/Instructions/ControlFlowInstructions.cs:601
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context (System.Object state) [0x0000e] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/threading/threadpool.cs:1304
at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x0008d] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:957
at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:904
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () [0x0002a] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/threading/threadpool.cs:1281
at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00096] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/threading/threadpool.cs:854
at ObjCRuntime.Runtime.ThreadPoolDispatcher (System.Func`1[TResult] callback) [0x00006] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/src/ObjCRuntime/Runtime.cs:238
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x0000c] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/threading/threadpool.cs:1206
Probably a Xamarin bug.
An Xamarin bug .. That only breaks when a new version of automapper is installed. Possibly but if you are going to look at it that way then lets just call it a iOS bug. Blame Apple and roll our own mapping systems for every project.
Any way we can reproduce this? Do you see this only on the device or in the simulator? I have Xamarin Studio so I can try to repro locally.
Let me see if I can Gut my project and zip it up for ya.
+1
We have also having this issue. Just like the OP, Android and UWP work but iOS fails. We were unable to successfully downgrade to 4.2.1 because of #1151. My attempts to create a small repro have been futile so far. I suspect a nuanced difference that I have not yet identified.
+1
It may be hard to repro, as it appears to blow up only on 12-th call and inside nested async methods...
What happens is that the interpreter compiles the expression after a given number of executions. So a repro should be easy to make, just run the list mapping inside a never-ending loop and wait for it to crash. After that, report it to the Mono team and see if they can help. A workaround is to set the compilation threshold to int.MaxValue, but I don't know if/how you can do that.
@jbogard I think we could workaround the bug by adding an extra block. This is silly stuff and certainly fragile, but doable. Another option is to ignore it. I think everything would work despite the exception. It would just interpret it every time. You get one unhandled exception per foreach expression on a thread pool thread. If the policy per process is to keep it running, that could work too :)
Ugh these all sound crappy.
Well, yes :) Unless you want to try to fix the Mono bug. Actually that's DLR code they borrowed.
+1
Filed a Bug with Xamarin (for what its worth) see: https://bugzilla.xamarin.com/show_bug.cgi?id=45141
Just got bitten by the same bug yesterday. I added a tiny iOS app to reproduce the bug, here's the copy-paste from the text submitted to the Xamarin bug:
When looking at the application output you'll never see then "Done" which should be printed right before the "return true;" in FinishedLaunching. To see the actual Exception the only way I found was to restart the application in Xamarin Studio. Only then you'll be able to see the stack trace in the application output window.
A repro should be possible without AutoMapper. A loop statement that doesn't have a block statement inside would have the same effect.
And it's indeed related to the CompilationThreshold - In the repro if you change the upper limit of the loop to 31 the problem is gone.
Just like @lbargaoanu assumed it, the issue is reproducible without AutoMapper and with a loop statement that doesn't have a block statement. The console output of the following snippet would count up to 1000 for the withoutIssue() lambda, and stop somewhere inbetween 32 and 100 for the withIssue() lambda. There's probably a race condition somewhere like stated in the DLR source
var breakLabel = Expression.Label("LoopBreak");
var counter = Expression.Variable(typeof(int), "counter");
var loopWithoutIssue = Expression.Block(new[] { counter },
Expression.Assign(counter, Expression.Constant(-1)),
Expression.Loop(
Expression.Block(Expression.IfThenElse(
Expression.Equal(Expression.Assign(counter, Expression.Add(counter, Expression.Constant(1))), Expression.Constant(1000)),
Expression.Block(Expression.Break(breakLabel)),
Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }), counter)
)),
breakLabel)
);
var loopWithIssue = Expression.Block(new[] { counter },
Expression.Assign(counter, Expression.Constant(-1)),
Expression.Loop(
Expression.IfThenElse(
Expression.Equal(Expression.Assign(counter, Expression.Add(counter, Expression.Constant(1))), Expression.Constant(1000)),
Expression.Break(breakLabel),
Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }), counter)
),
breakLabel)
);
var withoutIssue = Expression.Lambda<Action>(loopWithoutIssue).Compile();
var withIssue = Expression.Lambda<Action>(loopWithIssue).Compile();
withoutIssue();
withIssue();
Given this reproduction a possible workaround would be to wrap the IfThenElse expression in ExpressionExtensions.cs into a Block - However I'm not completely sure about the possible implications - Maybe it should only be done on iOS?
We've talked about this above. And I tend to agree, the root cause should be fixed.
Turns out the fix to LoopCompiler was quiet straightforward. Problem is they use a fork of the DLR, so I'm not really sure on how their policy is on fixes to the forked DLR code. I created a pull request https://github.com/mono/mono/pull/3759, we'll see what happens.
As a workaround until the PR is accepted and everything is released it is possible to reference a custom version of Mono.Dynamic.Interpreter.dll in affected iOS projects. I don't want to go through creating a NuGet for it (yet), so if anybody is interested I could provide a compiled version.
good god what a rabbit hole this is
For my own project and since first requests already came in, here's a Nuget on my Myget feed.
https://www.myget.org/feed/ddotmunch/package/nuget/Mono.Dynamic.Interpreter
It has version 2.0.5.1 as compared to the monotouch framework assembly which has version 2.0.5.0.
Feed details:
https://www.myget.org/F/ddotmunch/api/v3/index.json
The nuget package from @dmunch resolved this exception for me.
@dmunch help appreciated!
Gosh I was driving crazy looking the root cause of this issue. @dmunch Thank you!
+1
@kyurkchyan there actually is a +1 to comments btw :)
Hi guys, I tried to fix installing the nuget but keeps telling me the same error, I only have to install this in iOS project right? do I need to do something else? thanks!
any idea? @dmunch do I nedd to do something else to fix it? thanks
Hi @keozx, yeah normally installing it into the iOS project (the app project) should fix the error. I can't remember having done anything else in the Linker or Compiler settings.
ok don't know why is not working, maybe if I use the code directly? thanks @dmunch
@keozx, I think your Linker behavior must be set to "Don't Link", otherwise I'd guess that the Xamarin Linker just strips out the DLL since it's not used directly anywhere.
Seems to be fixed on mono master according to https://github.com/mono/mono/pull/3759#issuecomment-268086509
thanks @dmunch looks like even disabling linking it's not working, maybe that fix will be included in next patch
at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00038] in /Users/builder/data/lanes/3988/e02d2723/source/xamarin-mac
ios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:305
@keozx Are you sure it's the same issue? The stack trace looks different to me
@dmunch what the linker .config or Presever .cs file do you use? I encountered similar issue and tried your package and still get some random crashes with automapper on iOS with Link SDK Assembly option. I wanted try same link config with yours and see if I still get the crashes
@jamesatgithub if you're referencing to https://github.com/AutoMapper/AutoMapper/issues/1846 I actually don't think it's related.
@dmunch not actually, the issue #1846 could be resolved by linker configuration I think - I haven't tried it yet. While my mapping does have a IList so I tried your package but still got some crashes related to mapping so I used manual mapping finally in my iOS app.
But I do want to use AM (although I'm new to it) so I'm trying to figure out workaround to all blocks:
@jamesatgithub from my point of view any linker configuration should work, and there shouldn't be any additional packages needed for AutoMapper on iOS. The one provided by me is an exception and solely a workaround until the fix in mono master lands in a stable release of mono.
Furthermore, the fix in mono master actually replaces the whole dynamic interpreter which could mean that any other not yet filed bugs related to AutoMapper on iOS might be solved with it.
And if there's still issues with it you should open an issue with mono directly since in the end it's not really an issue of AutoMapper anyway. I'm actually positively surprised that the actual issue got addressed rather quickly by the mono team.
@dmunch Thanks.
Just to double check for "any linker configuration should work", the linker configuration I meant is https://developer.xamarin.com/guides/cross-platform/advanced/custom_linking/
do you use any xml configuration or .cs file with the Preserver attribute to get AM working without issues in iOS (Link SDK Assembly Only) build?
@jamesatgithub no didn't need any of those for AutoMapper yet.
@dmunch looks like yes indeed my error is now a different one, the linker was working properly using linkskip for the dll library. so this is an error that may affect the current mono as well.
On the other hand I tried to use the latest mono build (4.9) in the mac but now I get XamlC error don't know why, it builds using a previous mono build. I will keep looking into it and post details of the other error that the dll doesn't fix.
Do you know how can I tell if from VS in windows it is taking the mono I built in the mac server?
Just to keep everybody posted, the issue should be resolved with the latest Xamarion.iOS stable release (10.10.0.33).
Good to know :)
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
For my own project and since first requests already came in, here's a Nuget on my Myget feed.
https://www.myget.org/feed/ddotmunch/package/nuget/Mono.Dynamic.Interpreter
It has version 2.0.5.1 as compared to the monotouch framework assembly which has version 2.0.5.0.
Feed details:
https://www.myget.org/F/ddotmunch/api/v3/index.json