什么是使 iOS 应用程序崩溃的可靠方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13510584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's a reliable way to make an iOS app crash?
提问by Nestor
I want to test my app's crash reporting out in the field by deliberately having it crash when the user performs a particular action that a real user is unlikely to do accidentally.
我想通过在用户执行真实用户不太可能意外执行的特定操作时故意让它崩溃来测试我的应用程序在现场的崩溃报告。
But what's a good reliable way of making the app crash that doesn't create a warning at compile time?
但是,在编译时不会产生警告的使应用程序崩溃的可靠方法是什么?
Edit:Note that many seemingly obvious answers to this question result in exceptions that get caught by Cocoa and thus don't result in the app crashing.
编辑:请注意,这个问题的许多看似显而易见的答案会导致异常被 Cocoa 捕获,因此不会导致应用程序崩溃。
回答by Daij-Djan
in Objective-C use C directly to cause a bad access
在 Objective-C 中直接使用 C 导致访问错误
strcpy(0, "bla");
Note: while this works on any system I know -- in a future version of the C runtime OR the compiler this might not lead to a crash anymore. see Is null pointer dereference undefined behavior in Objective-C?)
注意:虽然这适用于我知道的任何系统 - 在 C 运行时或编译器的未来版本中,这可能不再导致崩溃。请参阅Objective-C 中的空指针取消引用未定义的行为吗?)
(in swift you would have to bridge to objC to do this)
(在 swift 你必须桥接到 objC 才能做到这一点)
回答by djromero
My current favourite:
我目前最喜欢的:
assert(! "crashing on purpose to test <insert your reason here>");
A classic:
一个经典:
kill( getpid(), SIGABRT );
And some pr0n:
还有一些 pr0n:
*(long*)0 = 0xB16B00B5;
All of them generate crashes captured by my crash reporting tool.
所有这些都会生成由我的崩溃报告工具捕获的崩溃。
回答by Dietrich Epp
Since we all use Clang for iOS, this is fairly reliable:
因为我们都在 iOS 上使用 Clang,所以这是相当可靠的:
__builtin_trap();
This has the benefit that it's designed for exactly this purpose, so it shouldn't generate any compiler warnings or errors.
这样做的好处是它专为此目的而设计,因此它不应生成任何编译器警告或错误。
回答by kmkaplan
abort();
causes abnormal termination… That is a crash.
abort();
导致异常终止……那是崩溃。
回答by Taum
How about a good old stack overflow :)
一个好的旧堆栈溢出怎么样:)
- (void)stackOverflow
{
[self stackOverflow];
}
回答by wirrwarr
Most popular one - unrecognised selector crash:
最受欢迎的一个 - 无法识别的选择器崩溃:
NSObject *object = [[NSObject alloc] init];
[object performSelector:@selector(asfd)];
Make sure you don't have -asdf method implemented in that class haha
确保您没有在该类中实现 -asdf 方法哈哈
Or index beyond bound exception:
或索引超出界限异常:
NSArray * array = [NSArray array];
[array objectAtIndex:5];
And of course
kill( getpid(), SIGABRT );
而且当然
kill( getpid(), SIGABRT );
回答by borchero
I think in Swift you could easily throw a fatal error:
我认为在 Swift 中你很容易抛出一个致命错误:
func foo() {
fatalError("crash!")
}
It is actually even intended to use this feature in case something goes wrong in order to make the app crash.
它实际上甚至打算在出现问题时使用此功能以使应用程序崩溃。
To avoid an if statement in a special case, you could use precondition
, too. It's similar to assert
, makes thus the intention (if wanted) pretty clear and is notremoved in the final release as assert
. It is used like precondition(myBoolean, "This is a helpful error message for debugging.")
.
为了避免在特殊情况下使用 if 语句,您也可以使用precondition
。它类似于 assert
,因此意图(如果需要)非常明确,并且不会在最终版本中作为assert
. 它像precondition(myBoolean, "This is a helpful error message for debugging.")
.
回答by Andrey Chernukha
Send a message to a deallocated object
向释放的对象发送消息
回答by Steve Rogers
exit(0);
(must... type... 30 characters)
(必须...输入... 30 个字符)
回答by Alessandro Vendruscolo
You can also raise an exception:
您还可以引发异常:
[NSException raise:NSInternalInconsistencyException
format:@"I want to test app crashes!."];