Xamarin.Mac模糊了SIGSEGV

时间:2014-10-21 20:06:12

标签: c# macos cocoa xamarin xamarin.mac

我遇到了xamarin.mac的一个模糊的段错误,这是(无用的)堆栈跟踪:

  at <unknown> <0xffffffff>
  at (wrapper managed-to-native) MonoMac.AppKit.NSApplication.NSApplicationMain     (int,string[]) <0xffffffff>
  at MonoMac.AppKit.NSApplication.Main (string[]) <0x00097>
  at gitbookpro.mac.MainClass.Main (string[]) <0x00017>
  at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <0xffffffff>

SelectionDidChange处理NSOutlineView并执行相当数量的处理后发生崩溃。

很难确定导致此次崩溃的确切原因。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

错误是由C#对象被错误地收集引起的。

它们被垃圾收集,因为这些对象被返回到objective-c代码(本机代码),并且因为C#没有引用,垃圾收集器正在删除它们。

这就是发生的事情:

1. create C# obj 2. return obj to native code 3. ... wait a little bit ... 4. turn native object back into to C# obj (in event handlers etc ...) 5. Access C# obj <= This would fail occasionally since it was being garbage collected during step #3

你应该做什么:

1. create C# obj 1bis. Keep an extra reference to the object somewhere (in an Dictionary for example) 2. return obj to native code 3. ... wait a little bit ... 4. turn native object back into to C# obj (in event handlers etc ...) 4bis. Remove extra reference 5. Access C# obj <= This would fail occasionally since it was being garbage collected during step #3

就是这样!