I have a MonoTouch tab view application. On one of my tabs, when a user clicks a button, I want to show another view. I do this with the following code:
UIView.BeginAnimations("flip");
UIView.SetAnimationDuration(1);
UIView.SetAnimationTransition(UIViewAnimationTransition.FlipFromRight, View, true);
NewFilesViewController newFilesViewController = new NewFilesViewController();
newFilesViewController.View.Frame = new System.Drawing.RectangleF(View.Frame.Top, this.View.Frame.Left, this.View.Frame.Width, this.View.Frame.Height);
View.AddSubview(newFilesViewController.View);
UIView.CommitAnimations();
On the new view, when I click a button, I get an error:
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
Should I be adding the view to the window instead? Is there a better way to do this?
This is likely (the code does not have enough context to be 100% sure) because newFilesViewController
is not referenced anywhere later in the code. As such it can be disposed the next time the Garbage Collector (GC) is invoked. However the native code still except the view to exists and this will crash when it tries to call the (disposed) instance.
Fix: Promote your newFilesViewController
local variable to a field. That will keep the reference alive (as long as the type's instance is alive) and the GC won't collect it.