C# NSActionDispatcher在连接仪器时被垃圾收集

C# NSActionDispatcher在连接仪器时被垃圾收集,c#,ios,mono,xamarin.ios,instruments,C#,Ios,Mono,Xamarin.ios,Instruments,我正在尝试向iOS应用程序添加计时器,为此,我使用以下代码: 使用系统; 使用系统图; 使用单调的基础; 使用MonoTouch.UIKit; 名称空间MyNamespace { [注册(“AppDelegate”)] 公共部分类AppDelegate:UIApplicationDelegate { 窗口; 公共静态导航控制器MyNavigationController; 公共覆盖bool FinishedLaunching(UIApplication应用程序、NSDictionary选项) {

我正在尝试向iOS应用程序添加计时器,为此,我使用以下代码:

使用系统;
使用系统图;
使用单调的基础;
使用MonoTouch.UIKit;
名称空间MyNamespace
{
[注册(“AppDelegate”)]
公共部分类AppDelegate:UIApplicationDelegate
{
窗口;
公共静态导航控制器MyNavigationController;
公共覆盖bool FinishedLaunching(UIApplication应用程序、NSDictionary选项)
{
矩形F边界=UIScreen.MainScreen.bounds;
MyNavigationController=新的NavigationController(边界);
窗口=新的UIWindow(边界);
window.RootViewController=MyNavigationController;
window.MakeKeyAndVisible();
返回true;
}
}
公共类NavigationController:UINavigationController
{
私有矩形f_界;
私有矩形F_视口;
公共导航控制器(矩形边界)
{
_界限=界限;
NavigationBar.BarStyle=UIBarStyle.Black;
}
公共覆盖无效ViewDidLoad()
{
base.ViewDidLoad();
_viewPort=new RectangleF(_-bounds.Location,new-SizeF(_-bounds.Width,_-bounds.Height-NavigationBar.Frame.Height));
ShowFirstView();//默认为显示FirstViewController
}
public void ShowFirstView()
{
PushViewController(新的FirstViewController(_视口),true);
}
public void ShowSecondView()
{
PushViewController(新的SecondViewController(_视口),true);
}
}
公共类FirstViewController:UIViewController
{
公共FirstViewController(矩形边界)
{
//空视图
视图=新的UIView(边界);
View.BackgroundColor=UIColor.White;
Title=“第一视图”;
//导航到下一个视图的简单按钮
UIButton=UIButton.FromType(UIButtonType.RoundedRect);
按钮框架=新矩形F(10,10300,30);
按钮.SetTitle(“显示下一个视图”,uicontrol状态.Normal);
button.TouchUpInside+=(发件人,e)=>{AppDelegate.MyNavigationController.ShowSecondView();};
View.AddSubview(按钮);
}
}
公共类SecondViewController:UIViewController
{
专用非定时定时器;
public SecondViewController(矩形边界)
{
//空视图
视图=新的UIView(边界);
View.BackgroundColor=UIColor.Blue;
Title=“第二视图”;
//创建计时器
_timer=NSTimer.CreateRepeatingScheduledTimer(2f,委托{Console.WriteLine(“计时器已触发”);});
}
}
}
当我在模拟器中运行这段代码时,它会像预期的那样工作,每2秒向控制台写入“Timer Fired”

但是,当我附加Apple Instruments时,特别是分配工具会在
UIApplication.Main(args,null,“AppDelegate”)抛出异常在Main.cs中

< p>选择器从对象MonoTouch.Foundation.NSActionDispatcher上调用的对象类型(0x10aa2d50)已被gc'Ed/p> 堆栈跟踪:

System.Exception: Selector invoked from objective-c on a managed object of type MonoTouch.Foundation.NSActionDispatcher (0x14FB5AC0) that has been GC'ed ---> System.Exception: No constructor found for MonoTouch.Foundation.NSActionDispatcher::.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) [0x000f1] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Activator.cs:280
  at System.Activator.CreateInstance (System.Type type, System.Object[] args, System.Object[] activationAttributes) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Activator.cs:234
  at System.Activator.CreateInstance (System.Type type, System.Object[] args) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Activator.cs:229
  at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject (IntPtr ptr, IntPtr klass) [0x0000d] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:231
  --- End of inner exception stack trace ---
  at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject (IntPtr ptr, IntPtr klass) [0x00045] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:236
  at MonoTouch.ObjCRuntime.Runtime.GetNSObject (IntPtr ptr) [0x0001f] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:280
  at MonoTouch.ObjCRuntime.Runtime.GetNSObjectWrapped (IntPtr ptr) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:297
  at (wrapper native-to-managed) MonoTouch.ObjCRuntime.Runtime:GetNSObjectWrapped (intptr)
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
  at MyNamescape.Application.Main (System.String[] args) [0x00000] in Main.cs:17
需要注意的关键事项:

  • 在正常情况下(无仪器),应用程序在模拟器中正确运行
  • 该问题仅在分配工具对其进行监控时发生。如果使用其他仪器(如泄漏),则不会出现问题
  • 只有在设置计时器之前连接分配仪表时,才会出现此问题。如果我在设置计时器后连接分配工具,那么它将按预期工作
  • 分配工具在我的应用程序的其他视图控制器中工作
  • 我在MonoTouch中找不到关于
    NSActionDispatcher
    与此上下文相关的更多信息
  • 我目前无法在物理设备上进行测试
因此,我不确定这是否是分配工具的问题,或者我的代码中是否存在严重错误。对此问题的任何建议都将不胜感激

编辑:我已经更新了代码,以显示导致问题的最简单完整的应用程序用例。(我已根据PouPou关于不必要使用线程的评论更新了计时器代码)

更新(2013年1月17日):我曾使用iPad和iPhone模拟器版本5.0、5.1和6.0尝试过这一点,它们都产生了相同的结果

更新(2013年1月18日):根据Rolf的要求添加本机堆栈帧

2013-01-18 13:28:47.579 TestTimer[12196:c07]   at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject (IntPtr ptr, IntPtr klass) [0x00045] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:236 
  at MonoTouch.ObjCRuntime.Runtime.GetNSObject (IntPtr ptr) [0x0001f] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:280 
  at MonoTouch.ObjCRuntime.Runtime.GetNSObjectWrapped (IntPtr ptr) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:297 
  at (wrapper native-to-managed) MonoTouch.ObjCRuntime.Runtime:GetNSObjectWrapped (intptr)
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38 
  at MyNamescape.Application.Main (System.String[] args) [0x00000] in Main.cs:16 
2013-01-18 13:28:47.623 TestTimer[12196:c07] 0   TestTimer                           0x00093a22 mono_handle_exception_internal_first_pass + 3058
1   TestTimer                           0x00095102 mono_handle_exception_internal + 1602
2   TestTimer                           0x00095c4f mono_handle_exception + 47
3   TestTimer                           0x000dda72 mono_x86_throw_exception + 306
4   ???                                 0x0b3e0f8f 0x0 + 188616591
at MonoTouch.ObjCRuntime.Runtime.GetNSObject (intptr) [0x0001f] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:280
at MonoTouch.ObjCRuntime.Runtime.GetNSObjectWrapped (intptr) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:297
at (wrapper native-to-managed) MonoTouch.ObjCRuntime.Runtime.GetNSObjectWrapped (intptr) <IL 0x00017, 0x00094>
8   TestTimer                           0x00224873 get_managed_object_for_ptr + 115
9   TestTimer                           0x00229700 monotouch_trampoline + 448
10  Foundation                          0x019b9b90 __NSFireTimer + 97
11  CoreFoundation                      0x01315376 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
12  CoreFoundation                      0x01314e06 __CFRunLoopDoTimer + 534
13  CoreFoundation                      0x012fca82 __CFRunLoopRun + 1810
14  CoreFoundation                      0x012fbf44 CFRunLoopRunSpecific + 276
15  CoreFoundation                      0x012fbe1b CFRunLoopRunInMode + 123
16  GraphicsServices                    0x04b807e3 GSEventRunModal + 88
17  GraphicsServices                    0x04b80668 GSEventRun + 104
18  UIKit                               0x0273965c UIApplicationMain + 1211
at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <IL 0x00056, 0x001f5>
at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
at MyNamescape.Application.Main (string[]) [0x00000] in Main.cs:16
at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00049, 0x0014e>
23  TestTimer                           0x00010252 mono_jit_runtime_invoke + 722
24  TestTimer                           0x0017478e mono_runtime_invoke + 126
25  TestTimer                           0x00178be4 mono_runtime_exec_main + 420
26  TestTimer                           0x00178f55 mono_runtime_run_main + 725
27  TestTimer                           0x0006ba65 mono_jit_exec + 149
28  TestTimer                           0x0021f65d main + 2013
29  TestTimer                           0x00003125 start + 53
2013-01-18 13:28:47.579测试计时器[12196:c07]在/Developer/MonoTouch/Source/MonoTouch/src/ObjCRuntime/Runtime.cs:236中
在/Developer/MonoTouch/Source/MonoTouch/src/ObjCRuntime/Runtime.cs:280中的MonoTouch.ObjCRuntime.Runtime.GetNSObject(IntPtr ptr)[0x0001f]处
在/Developer/MonoTouch/Source/MonoTouch/src/ObjCRuntime/Runtime.cs:297中的MonoTouch.ObjCRuntime.Runtime.GetNSObjectWrapped(IntPtr ptr)[0x00000]处
at(包装器本机到托管)MonoTouch.ObjCRuntime.Runtime:GetNSObjectWrapped(intptr)
at(包装器管理为本机)MonoTouch.UIKit.UIApplication:UIApplicationMain(int,string[],intptr,intptr)
在/Developer/MonoTouch/Source/MonoTouch/src/UIKit/UIApplication.Main中的MonoTouch.UIKit.UIApplication.Main(System.String[]args,System.String principalClassName,System.String delegateClassName)[0x0004c]
在Main.cs:16中的MyNamescape.Application.Main(System.String[]args)[0x00000]处
2013-01-18 13:28:47.623测试计时器[12196:c07]0测试计时器0x00093a22单声道句柄异常内部首次通过+3058
1个测试计时器