If I write such a method, the viewcontroller will be garbage collected:
public void PushShowTeamController (Object a)
{
var teamController = new TeamController (a);
NavigationController.PushViewController (teamController, true);
}
Because the iPod suddenly throws this error:
Jan 6 18:52:09 unknown MyApp[5197]: Received memory warning .
Jan 6 18:52:10 unknown UIKitApplication:mypackage.app[0x9db2][5197]: Unhandled Exception: System.Exception: Selector invoked from objective-c on a managed object that has been GC' ed ---> System.MissingMethodException: No constructor found for MyApp.TeamController::.ctor(System.IntPtr)
Jan 6 18:52:10 unknown UIKitApplication:mypackage.app[0x9db2][5197]: at System.Activator.CreateInstance (System.Type type, BindingFlags bindingAttr, System.Reflection.Binder bind er, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes) [0x00000] in :0
Jan 6 18:52:10 unknown UIKitApplication:mypackage.app [0x9db2][5197]: at System.Activator.CreateInstance (System.Type type, System.Object[] args, System.Object[] activationAttributes) [0x00000] in :0
Jan 6 18:52:10 unknown UIKitApplication:mypackage.app[0x9db2][5197]: at System.Activator.CreateInstance (System.Type type, System.Object[] args) [0x00000] in :0
Jan 6 18:52:10 unknown UIKitApplication:mypackage.app[0x9db2][5197]: at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject (IntPtr ptr, IntPtr klass) [0x00000] in :0
Jan 6 18:52:10 unknown UIKitApplication:mypackage.app[0x9db2][5197]: --- End of inner exception stack trace ---
You need to make sure to keep a reference to MyApp.TeamController in the managed code, as long as the native code can (e.g. access )It.
In most cases, MonoTouch will make sure to keep the reference. Your code:
NavigationController.PushViewController (teamController, true);< /pre>is correct, UINavigationController will keep a reference to teamController. However, if PushViewController is called on another controller, the first reference will be overwritten and can be collected by the garbage collector (GC). This This situation will lead to the exceptions you see.
Note: Because you cannot predict when the GC will collect unreferenced object instances, exceptions will be randomly thrown.
We have an app in the appstore that has no problems on iPhone 4g/4gs, but when I tested it on iPod, I got a surprise because it kept crashing in "random" places. Looking at the traces of strack, my vision controller seems to have passed GC.
If I write such a method, the viewcontroller will be garbage collected:
public void PushShowTeamController (Object a)
{
var teamController = new TeamController (a);
NavigationController.PushViewController (teamController, true);
}
Because the iPod suddenly throws this error:
Jan 6 18:52:09 unknown MyApp[5197]: Received memory warning.
Jan 6 18:52:10 unknown UIKitApplication:mypackage.app[0x9db2][5197]: Unhandled Exception: System.Except ion: Selector invoked from objective-c on a managed object that has been GC'ed ---> System.MissingMethodException: No constructor found for MyApp.TeamController::.ctor(System.IntPtr)
Jan 6 18: 52:10 unknown UIKitApplication:mypackage.app[0x9db2][5197]: 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 :0
Jan 6 18:52:10 unknown UIKitApplication:mypackage.app[0x9db2][5197]: at System.Activator.CreateInstance (System.Type type, System.Object[] args, System.Object[] activationAttributes) [0x00000] in :0
Jan 6 18:52:10 unknown UIKitApplication:mypackage .app[0x9db2][5197]: at System.Activator.CreateInstance (System.Type type, System.Object[] args) [0x00000] in :0
Jan 6 18:52 :10 unknown UIKitApplication :mypackage.app[0x9db2][5197]: at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject (IntPtr ptr, IntPtr klass) [0x00000] in :0
Jan 6 18:52:10 unknown UIKitApplication:mypackage.app[0x9db2][5197]: --- End of inner exception stack trace ---
When dealing with managed objects This happens when there is no reference to the instance, and the object is later resurfaced by native code.
You need to ensure that the MyApp .TeamController reference, as long as the native code can (e.g. access) it.
In most cases, MonoTouch will make sure to keep the reference. Your code:
NavigationController.PushViewController (teamController, true);
is correct, UINavigationController will maintain a reference to teamController. However, if PushViewController is called on another controller, the first reference will be Covered, and can be collected by the garbage collector (GC). This situation will cause the exceptions you see.
Note: Because you cannot predict when the GC will collect unreferenced object instances, the exception will be Are randomly thrown.