iOS中有大量iOS崩溃报告库,包括TestFlight和HockeyApp。如果您不想依赖服务,您仍然可以使用PLCrashReporter之类的库。绑定这些库是fairly trivial,因为它们的公共API通常由几个具有多种初始化方法的类组成。
然而,当我们在我们的应用程序中尝试使用TestFlight和更高版本的HockeyApp时,我们的应用程序开始随机崩溃。事实证明,这个 是一个已知问题reported several times,但是Xamarin没有警告它,它相对模糊,我们发现它很难方式。
我们了解到所有iOS崩溃记录器都阻止Mono捕获空引用异常:
try {
object o = null;
o.GetHashCode ();
} catch {
// Catch block isn't called with crash reporting enabled.
// Instead, the app will crash.
}
为什么会这样?引用Xamarin开发人员Rolf,
空引用异常实际上是SIGSEGV信号。通常是 mono运行时处理它并将其转换为null引用异常, 允许执行继续。问题是SIGSEGV信号是a 在ObjC应用程序中非常糟糕(当它发生在托管代码之外时),所以 任何崩溃报告解决方案都会将其报告为崩溃(并杀死应用程序) - 这是在MonoTouch有机会处理SIGSEGV之前发生的,所以有 MonoTouch无法做到这一点。
我确信很多人在MonoTouch应用程序中使用TestFlight而不知道它会导致崩溃 Isn't it ironic?
如何使崩溃报告库不崩溃MonoTouch应用程序?
答案 0 :(得分:58)
将其放入AppDelegate.cs
:
[DllImport ("libc")]
private static extern int sigaction (Signal sig, IntPtr act, IntPtr oact);
enum Signal {
SIGBUS = 10,
SIGSEGV = 11
}
static void EnableCrashReporting ()
{
IntPtr sigbus = Marshal.AllocHGlobal (512);
IntPtr sigsegv = Marshal.AllocHGlobal (512);
// Store Mono SIGSEGV and SIGBUS handlers
sigaction (Signal.SIGBUS, IntPtr.Zero, sigbus);
sigaction (Signal.SIGSEGV, IntPtr.Zero, sigsegv);
// Enable crash reporting libraries
EnableCrashReportingUnsafe ();
// Restore Mono SIGSEGV and SIGBUS handlers
sigaction (Signal.SIGBUS, sigbus, IntPtr.Zero);
sigaction (Signal.SIGSEGV, sigsegv, IntPtr.Zero);
Marshal.FreeHGlobal (sigbus);
Marshal.FreeHGlobal (sigsegv);
}
static void EnableCrashReportingUnsafe ()
{
// Run your crash reporting library initialization code here--
// this example uses HockeyApp but it should work well
// with TestFlight or other libraries.
// Verify in documentation that your library of choice
// installs its sigaction hooks before leaving this method.
var manager = BITHockeyManager.SharedHockeyManager;
manager.Configure (HockeyAppId, null);
manager.StartManager ();
}
在EnableCrashReporting ()
方法的开头致电FinishedLaunching
如果需要,请在#if !DEBUG
指令中包含此调用。
我遵循了罗尔夫的建议:
一种可能的解决方案是允许mono处理所有SIGSEGV信号 (从技术上讲,崩溃报告库应该不能处理 SIGSEGV信号,或它应链接到mono的处理程序而不是 任何处理本身)。如果mono确定SIGSEGV信号 不是来自托管代码(即发生了非常糟糕的事情),它会 提出一个SIGABORT信号(崩溃报告库应该已经存在) 处理和处理崩溃)。你可以理解这是件事 必须在崩溃报告库中完成。
和Landon Fuller的Objective C实现:
#import <signal.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/* Save Mono's signal handler actions */
struct sigaction sigbus_action, sigsegv_action;
sigaction(SIGBUS, NULL, &sigbus_action);
sigaction(SIGSEGV, NULL, &sigsegv_action);
// Enable the crash reporter here. Ie, [[PLCrashReporter sharedReporter] enableCrashReporterAndReturnError:],
// or whatever is the correct initialization mechanism for the crash reporting service you're using
/* Restore Mono's signal handlers */
sigaction(SIGBUS, &sigbus_action, NULL);
sigaction(SIGSEGV, &sigsegv_action, NULL);
return YES;
}
我使用Banshee source code作为如何从MonoTouch调用sigaction
的参考点。
希望它有所帮助!
答案 1 :(得分:4)
从Xamarin.iOS 10.4开始,现在有一种支持的方式:
static void EnableCrashReporting ()
{
try {
} finally {
Mono.Runtime.RemoveSignalHandlers ();
try {
EnableCrashReportingUnsafe ();
} finally {
Mono.Runtime.InstallSignalHandlers ();
}
}
}
static void EnableCrashReportingUnsafe ()
{
// Run your crash reporting library initialization code here--
// this example uses HockeyApp but it should work well
// with TestFlight or other libraries.
// Verify in documentation that your library of choice
// installs its sigaction hooks before leaving this method.
// Have in mind that at this point Mono will not handle
// any NullReferenceExceptions, if there are any
// NullReferenceExceptions on any thread (not just the current one),
// then the app will crash.
var manager = BITHockeyManager.SharedHockeyManager;
manager.Configure (HockeyAppId, null);
manager.StartManager ();
}