MvvmCross - Error when loading ViewModel - mvvmcross

I'm testing out MvvmCross in an existing Monotouch project.
I would like to start migrating the project to Mvvm in pieces.
I can start the application and it shows my first (mvvm) screen.
After that, i'm doing different (non mvvmcross) things (loading views etc) which I would like to port on a later moment.
I was now trying to port one of my views also to mvvmcross.
I created a View "public class TestView : MvxViewController" and a ViewModel "public class TestViewModel : MvxViewModel"
When loading that View, the app crashed with following error:
---Message:
Object reference not set to an instance of an object
---Stack Trace:
at Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request, IMvxBundle savedState) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Touch.Views.MvxViewControllerExtensionMethods.LoadViewModel (IMvxTouchView touchView) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Touch.Views.MvxViewControllerExtensionMethods+<OnViewCreate>c__AnonStorey3.<>m__9 () [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Views.MvxViewExtensionMethods.OnViewCreate (IMvxView view, System.Func`1 viewModelLoader) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Touch.Views.MvxViewControllerExtensionMethods.OnViewCreate (IMvxTouchView touchView) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Touch.Views.MvxViewControllerAdapter.HandleViewDidLoadCalled (System.Object sender, System.EventArgs e) [0x00000] in <filename unknown>:0
at (wrapper delegate-invoke) <Module>:invoke_void__this___object_EventArgs (object,System.EventArgs)
at Cirrious.CrossCore.Touch.Views.MvxDelegateExtensionMethods.Raise (System.EventHandler eventHandler, System.Object sender) [0x00000] in <filename unknown>:0
at Cirrious.CrossCore.Touch.Views.MvxEventSourceViewController.ViewDidLoad () [0x00000] in <filename unknown>:0
at CatalogMVVMTest.Touch.Views.TestView.ViewDidLoad () [0x0001a] in /Users/matthiasvalcke/Projects/MVVMDemos/CatalogMVVMTest/CatalogMVVMTest.Touch/Views/TestView.cs:20
...
It seems it crashes when calling "base.ViewDidLoad ();"
Not sure if this is important, but after loading the first view, I'm loading a new (non mvvmcross) view as root of the navigation stack.
So now I'm trying to load a MvvmCross View to a normal UIView(Controller)
I'm also getting following Mvx diagnostic output:
mvx: Diagnostic: 1.08 Request is null - assuming this is a TabBar type situation where ViewDidLoad is called during construction... patching the request now - but watch out for problems with virtual calls during construction
edit:
I'm also getting it if I just edit a simple sample from MvvmCross. For example, if I just take the basic sample with one "FirstView" and "FirstViewModel" and I do something like this, I'm getting the error:
var secondv = new SecondView ();
secondv.View.Frame = new RectangleF (0,250,320,100);
Add (secondv.View);
SecondView is just a MvxViewController. It seems like I can not add a View from a MvxViewController to another MvxViewController in code. Is this correct?
Anyone having an idea?

Ok, instead of doing this:
var secondv = new SecondView (); //(this is a MvxViewController
secondv.View.Frame = new RectangleF (0,250,320,100);
Add (secondv.View);
I had to do this (i found it in the tabbar sample):
var viewModel = new SecondViewModel ();
var secondv = this.CreateViewControllerFor(viewModel) as UIViewController;
secondv.View.Frame = new RectangleF (0,250,320,10);
secondv.View.AutoresizingMask = UIViewAutoresizing.None; //was needed because mvvm is enabling autostretch automatically?
Add (secondv.View);
Sorry for the questions, I'm using a lot of UIViewControllers on other UIViewControllers and it's difficult to understand the complete MvvmCross framework :-)
I'm still getting following diagnostic output, but no nullpointer anymore and it works:
mvx: Diagnostic: 0.11 Request is null - assuming this is a TabBar type situation where ViewDidLoad is called during construction... patching the request now - but watch out for problems with virtual calls during construction
Thanks for the help!

You are most likely hitting a bug in Xamarin's latest release.
This was addressed in https://github.com/slodge/MvvmCross/commit/5786d4964e4262cbf91661f04427e19648acf7df
Try upgrading to the mvvmcross 3.0.11-beta1 release or later - or roll back your xamarin version.
More info on http://forums.xamarin.com/discussion/comment/26049/#Comment_26049

Related

ServiceStack System.IndexOutOfRangeException at JsvTypeSerializer.EatMapKey

In my logs, I found a weird error regarding my ServiceStack service. I don't have further information than the following stacktrace and I didn't manage to reproduce the error yet. That's the stacktrace:
JsvTypeSerializer.EatMapKey (ServiceStack.Text.StringSegment value, System.Int32& i)
DeserializeDictionary`1[TSerializer].ParseStringDictionary (ServiceStack.Text.StringSegment value)
(wrapper delegate-invoke) :invoke_object_StringSegment (ServiceStack.Text.StringSegment)
JsvReader`1[T].ParseStringSegment (ServiceStack.Text.StringSegment value)
JsvReader`1[T].Parse (System.String value)
TypeSerializer.DeserializeFromString[T] (System.String value)
StringExtensions.FromJsv[T] (System.String jsv)
WebServiceException.ParseResponseDto ()
WebServiceException.get_ErrorMessage ()
WebServiceException.get_Message ()
I'm not sure where I should start, the service actually only has json enabled and not jsv, and the part where I handle the request is inside a try-catch block, so I'm not sure why the error is actually happening.
This Exception is due to not being able to parse a structured Error ResponseStatus thrown in a WebServiceException possibly due to returning an unknown Error Response. I've made it so this parsing heuristic doesn't throw an Exception when it fails in this commit.
This change is available from v5.0.3 that's now available on MyGet.

Is GroupMailBox missing from enum MailboxType?

We’re have a sporadic problem with EWS mail users. In the stack trace we see
System.ArgumentException: Requested value 'GroupMailbox' was not found.'
Findings
This is the StackTrace :
at System.Enum.TryParseEnum (System.Type enumType, System.String value, Boolean ignoreCase, System.EnumResult& parseResult) <0x10087d640 + 0x0052b>
in <filename unknown>:0
at System.Enum.Parse (System.Type enumType, System.String value, Boolean ignoreCase) <0x1006b91a8 + 0x00057>
in <filename unknown>:0
at Microsoft.Exchange.WebServices.Data.EwsUtilities.Parse[T] (System.String value) <0x10114e1ac + 0x000e3>
in <filename unknown>:0
at Microsoft.Exchange.WebServices.Data.EwsXmlReader.ReadValue[T] () <0x10114e764 + 0x00053>
in <filename unknown>:0
at Microsoft.Exchange.WebServices.Data.EwsXmlReader.ReadElementValue[T] () <0x10114e078 + 0x00087>
in <filename unknown>:0
at Microsoft.Exchange.WebServices.Data.EmailAddress.TryReadElementFromXml (Microsoft.Exchange.WebServices.Data.EwsServiceXmlReader reader) <0x1010a4330 + 0x00187>
at Microsoft.Exchange.WebServices.Data.EmailAddress.TryReadElementFromXml (Microsoft.Exchange.WebServices.Data.EwsServiceXmlReader reader) <0x1010a4330 + 0x00187>
In the source we thing this method
EmailAddress :: TryReadElementFromXml
case XmlElementNames.MailboxType:
this.mailboxType = reader.ReadElementValue<MailboxType>();
Analysis:
We believe that for some messages a MailboxType enum is trying to be parsed. However the enum MailboxType does not contain the value GroupMailbox and therefore throws an exception.
This is the documentation for the MailboxType enum
https://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.mailboxtype%28v=exchg.80%29.aspx?f=255&MSPPError=-2147217396
Is it possible that the server logic was updated on the enum was not kept up-to-date???
Furthermore:
Also we’ve traced it down to the method in EWSUtilities.cs
internal static T Parse<T>(string value)
The else clause is
else
{
return (T)Enum.Parse(typeof(T), value, false);
}
Which should be reserved for when the enumeration is not found??? And instead of doing a TryParse its doing a Parse which fails.
This apparently is also a bug???
The Source for the EWS Managed API is on GitHub and I believe this has been updated with then new Enum https://github.com/OfficeDev/ews-managed-api/blob/154dbc66ac018d861c73ce489839cd9f58a1b0cd/Enumerations/MailboxType.cs . You should be compiling and using the latest source from GitHub as the latest release version and NuGet version released predates the change. (Microsoft should really be updating NuGet package to avoid issues).

Xamarin iOS throws System.TypeInitializationException when loading a large native library

I have an iOS app built with Xamarin iOS that crashes when loading the class that has DllImport attributes for PInvoke calls. The exception is:
Unhandled managed exception: An exception was thrown by the type initializer for Pazanga.Native.ZzPINVOKE (System.TypeInitializationException)
The app worked fine, but started crashing when the native library grew in size. After cropping some parts of the library it worked again, but after linking a new library it started crashing again. The crash happens before any calls are actually made to the native library.
Here is the device log after the crash: http://pastebin.com/vW3CMXHq
The inner exceptions are:
Aug 23 10:33:40 Outboxs-iPod pazanga[4216] <Warning>: System.TypeInitializationException: An exception was thrown by the type initializer for Pazanga.Native.ZzPINVOKE ---> System.TypeInitializationException: An exception was thrown by the type initializer for SWIGStringHelper ---> System.ExecutionEngineException: Attempting to JIT compile method '(wrapper native-to-managed) Pazanga.Native.ZzPINVOKE/SWIGStringHelper:CreateString (intptr)' while running with --aot-only. See http://docs.xamarin.com/ios/about/limitations for more information.
at (wrapper managed-to-native) object:__icall_wrapper_mono_delegate_to_ftnptr (object)
at (wrapper managed-to-native) Pazanga.Native.ZzPINVOKE/SWIGStringHelper:SWIGRegisterStringCallback_Zz (Pazanga.Native.ZzPINVOKE/SWIGStringHelper/SWIGStringDelegate)
at Pazanga.Native.ZzPINVOKE+SWIGStringHelper..cctor () [0x00017] in /Users/max/Code/pazanga/mobile/ios/bindings/ZzPINVOKE.cs:229
--- End of inner exception stack trace ---
at Pazanga.Native.ZzPINVOKE..cctor () [0x0000a] in /Users/max/Code/pazanga/mobile/ios/bindings/ZzPINVOKE.cs:233
--- End of inner exception stack trace ---
at Pazanga.Native.Zz.locate (System.Byte[] image, image_format format, Int32 width, Int32 height, Int32 x, Int32 y) [0x00009] in /Users/max/Code/pazanga/mobile/ios/bindings/Zz.cs:16
at Pazanga.CaptureDecode.ProcessFrame (System.Byte[] data, Int32 dataWidth, Int32 dataHeight, Boolean isPicture) [0x000cf] in /Users/max/Code/pazanga/mobile/common/CaptureDecode.cs:95
at Pazanga.iOS.CaptureViewController.HandleNewFrame (System.Byte[] data, Int32 width, Int32 height) [0x0003b] in /Users/max/Code/pazanga/mobile/ios/App/CaptureViewController.cs:128
at Pazanga.iOS.CaptureManager.OnNewFrame (System.Byte[] array, Int32 width, Int32 height) [0x00015] in /Users/max/Code/pazanga/mobile/ios/App/CaptureManager.cs:126
at Pazanga.iOS.CaptureManager+OutputDelegate.DidOutputSampleBuffer (MonoTouch.AVFoundation.AVCaptureOutput captureOutput, MonoTouch.CoreMedia.CMSampleBuffer sampleBuffer, MonoTouch.AVFoundation.AVCaptureConnection connection) [0x000c6] in /Users/max/Code/pazanga/mobile/ios/App/CaptureManager.cs:110
Aug 23 10:33:40 Outboxs-iPod pazanga[4216] <Warning>: System.TypeInitializationException: An exception was thrown by the type initializer for SWIGStringHelper ---> System.ExecutionEngineException: Attempting to JIT compile method '(wrapper native-to-managed) Pazanga.Native.ZzPINVOKE/SWIGStringHelper:CreateString (intptr)' while running with --aot-only. See http://docs.xamarin.com/ios/about/limitations for more information.
at (wrapper managed-to-native) object:__icall_wrapper_mono_delegate_to_ftnptr (object)
at (wrapper managed-to-native) Pazanga.Native.ZzPINVOKE/SWIGStringHelper:SWIGRegisterStringCallback_Zz (Pazanga.Native.ZzPINVOKE/SWIGStringHelper/SWIGStringDelegate)
at Pazanga.Native.ZzPINVOKE+SWIGStringHelper..cctor () [0x00017] in /Users/max/Code/pazanga/mobile/ios/bindings/ZzPINVOKE.cs:229
--- End of inner exception stack trace ---
at Pazanga.Native.ZzPINVOKE..cctor () [0x0000a] in /Users/max/Code/pazanga/mobile/ios/bindings/ZzPINVOKE.cs:233
Aug 23 10:33:40 Outboxs-iPod pazanga[4216] <Warning>: System.ExecutionEngineException: Attempting to JIT compile method '(wrapper native-to-managed) Pazanga.Native.ZzPINVOKE/SWIGStringHelper:CreateString (intptr)' while running with --aot-only. See http://docs.xamarin.com/ios/about/limitations for more information.
at (wrapper managed-to-native) object:__icall_wrapper_mono_delegate_to_ftnptr (object)
at (wrapper managed-to-native) Pazanga.Native.ZzPINVOKE/SWIGStringHelper:SWIGRegisterStringCallback_Zz (Pazanga.Native.ZzPINVOKE/SWIGStringHelper/SWIGStringDelegate)
at Pazanga.Native.ZzPINVOKE+SWIGStringHelper..cctor () [0x00017] in /Users/max/Code/pazanga/mobile/ios/bindings/ZzPINVOKE.cs:229
And here is the SWIGStringHelper class (generated by SWIG) that appears in the exception:
protected class SWIGStringHelper {
public delegate string SWIGStringDelegate(string message);
static SWIGStringDelegate stringDelegate = new SWIGStringDelegate(CreateString);
[DllImport("__Internal", EntryPoint="SWIGRegisterStringCallback_Zz")]
public static extern void SWIGRegisterStringCallback_Zz(SWIGStringDelegate stringDelegate);
[MonoTouch.MonoPInvokeCallback(typeof(SWIGStringDelegate))]
static string CreateString(string cString) {
return cString;
}
static SWIGStringHelper() {
SWIGRegisterStringCallback_Zz(stringDelegate);
}
}
Any ideas on how to solve this?
To close the loop... It turned out to be a duplicate of bug 14194 (reverse callback using a string).
A TypeInitializationException was thrown because the exception occurred inside a static constructor.

What are my options to hunt Monotouch exceptions like "System.Exception: Selector invoked from objective-c on a managed object that has been GC'ed"?

I'm getting some of those exceptions below. What are my options to track those issues down? I'm kind of lost with those as they happen sporadically only and ONLY on the device but never in the Simulator.
System.Exception: Selector invoked from objective-c on a managed object that has been GC'ed ---> System.MissingMethodException: No constructor found for Browser.FolderListController::.ctor(System.IntPtr)
at System.Activator.CreateInstance (System.Type type, BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes) [0x00000] in <filename unknown>:0
at System.Activator.CreateInstance (System.Type type, System.Object[] args, System.Object[] activationAttributes) [0x00000] in <filename unknown>:0
at System.Activator.CreateInstance (System.Type type, System.Object[] args) [0x00000] in <filename unknown>:0
at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject (IntPtr ptr, IntPtr klass) [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject (IntPtr ptr, IntPtr klass) [0x00000] in <filename unknown>:0
at MonoTouch.ObjCRuntime.Runtime.GetNSObject (IntPtr ptr) [0x00000] in <filename unknown>:0
at MonoTouch.ObjCRuntime.Runtime.GetNSObjectWrapped (IntPtr ptr) [0x00000] in <filename unknown>:0
at (wrapper native-to-managed) MonoTouch.ObjCRuntime.Runtime:GetNSObjectWrapped (intptr)
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00000] in <filename unknown>:0
at Browser.Application.Main (System.String[] args) [0x00000] in .../Main.cs:15
This isn't exactly related to the OP's situation, but I got this error when loading a view from a nib and it turned out I hadn't setup the view class's constructor properly:
public MyView() : base()
{
// Wrong!
}
You need to include the IntPtr parameter:
public MyView(IntPtr handle) : base(handle)
{
}
There's not a single, simple answer to that but I can share a way to help hunting them done:
Like #Miguel said such an exception means that the runtime is trying to re-surface an instance of Browser.FolderListController. That means that one, or many, instance(s) of Browser.FolderListController are being collected even if they will still be needed later.
Your first step should be to review the life cycle of every Browser.FolderListController instances. E.g.
where they are created;
where the calls occurs;
where you Dispose them manually - which can conflict with any retain'ing MonoTouch try to do to help your;
null'ed or removed from collections (i.e. removing references)...
Once you know the lifecycle you can:
add a finalizer to Browser.FolderListController and set a breakpoint inside it. The finalizer are executed on a separate thread so it won't tell you where the last reference was removed - but it will tell you approximately when (at least not before some point);
add the .ctor(IntPtr) constructor and add a breakpoint inside it. Again it won't give you an exact point (where it's being required) but it will tell you approximately when (at least not before another point);
Execute your application, get the crash and then look at what's going on between between those two points in time (compared to your lifecycle).
The above means that an object was created, passed down to Objective-C to be kept around and then it was garbage collected by Mono, and later re-surfaced to Mono on either a callback or some delegate invocation.
This usually means that in our code, we failed to catch a case where we needed to keep an explicit reference. A test case for this would be useful as it would help us fix this in MonoTouch.
We have also developed a new technique to eliminate all of these problems at once, but the code is not ready for public use.

Exception when creating new Iphone/Ipad window in MonoDevelop

I get this exception every time i try to create a new Iphone/Ipad solution?
I have been following the guides and have both XCode, interface builder and IOS SDK installed.
Any clues are welcome:)
System.ApplicationException: Can't create display binding for mime type: application/vnd.apple-interface-builder
at MonoDevelop.Ide.Gui.Workbench.NewDocument (System.String defaultName, System.String mimeType, System.IO.Stream content) [0x00000] in :0
at MonoDevelop.Ide.Templates.FileTemplate.CreateFile (MonoDevelop.Ide.Templates.FileDescriptionTemplate newfile, MonoDevelop.Projects.SolutionItem policyParent, MonoDevelop.Projects.Project project, System.String directory, System.String language, System.String name) [0x00000] in :0
at MonoDevelop.Ide.Templates.FileTemplate.Create (MonoDevelop.Projects.SolutionItem policyParent, MonoDevelop.Projects.Project project, System.String directory, System.String language, System.String name) [0x00000] in :0
at MonoDevelop.Ide.Projects.NewFileDialog.OpenEvent (System.Object sender, System.EventArgs e) [0x00000] in :0
It appears that you are trying to create a new file, not a new project/solution.