What's a reliable way to make an iOS app crash?

IphoneObjective CIos

Iphone Problem Overview


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.

Iphone Solutions


Solution 1 - Iphone

in Objective-C use C directly to cause a bad access

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 https://stackoverflow.com/questions/13651642/is-null-pointer-dereference-defined-behavior-in-objective-c)

(in swift you would have to bridge to objC to do this)

Solution 2 - Iphone

My current favourite:

assert(! "crashing on purpose to test <insert your reason here>");

A classic:

kill( getpid(), SIGABRT );

And some pr0n:

*(long*)0 = 0xB16B00B5;

All of them generate crashes captured by my crash reporting tool.

Solution 3 - Iphone

Since we all use Clang for iOS, this is fairly reliable:

__builtin_trap();

This has the benefit that it's designed for exactly this purpose, so it shouldn't generate any compiler warnings or errors.

Solution 4 - Iphone

How about a good old stack overflow :)

- (void)stackOverflow
{
    [self stackOverflow];
}

Solution 5 - Iphone

abort(); causes abnormal termination… That is a crash.

Solution 6 - Iphone

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

Or index beyond bound exception:

NSArray * array = [NSArray array];
[array objectAtIndex:5];

And of course kill( getpid(), SIGABRT );

Solution 7 - Iphone

I think in Swift you could easily throw a fatal error:

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 not removed in the final release as assert. It is used like precondition(myBoolean, "This is a helpful error message for debugging.").

Solution 8 - Iphone

Send a message to a deallocated object

Solution 9 - Iphone

exit(0);

(must... type... 30 characters)

Solution 10 - Iphone

You can also raise an exception:

[NSException raise:NSInternalInconsistencyException
            format:@"I want to test app crashes!."];

Solution 11 - Iphone

Add a gesture recognizer to a view that recognizes a 10 finger tap (5 fingers for iPhone as 10 can get a bit crowded). The GR has a method attached to it that executes anyone of the previously mentioned surefire ways to get your app to crash. Most users are not going to lay 10 fingers down on your app, so you're safe from the general user accidentally causing the crash.

However you should be able to use something like Testflight or just deploying it to personal devices and test in the wild before ever submitting it to Apple. Having a forced crash could get your app rejected by Apple.

Solution 12 - Iphone

could try something like

NSArray* crashingArray = [NSArray arrayWithCapacity:1];
[crashingArray release];

should crash on an EXC_BAD_ACCESS (might need to release it a second time but normaly it should crash like this already)

Solution 13 - Iphone

I will go with:int raise(int sig);

To get more info >man raise

Solution 14 - Iphone

I would just kill the process normally:

kill(getpid(), SIGKILL);

So if you install a handler with signal you can also handle the crash, finishing to write opened files and these things.

Solution 15 - Iphone

I use

[self doesNotRecognizeSelector:_cmd]; 

Solution 16 - Iphone

When working with RubyMotion I use this:

    n=Pointer.new ('c', 1)
    n[1000] ='h'

Solution 17 - Iphone

Try this:

- (IBAction)Button:(id)sender
{
    NSArray *array = [NSArray new];
    NSLog(@"%@",[array objectAtIndex:8]);
}

Solution 18 - Iphone

a wrong NSLog statement will do it

NSLog(@"%@",1);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNestorView Question on Stackoverflow
Solution 1 - IphoneDaij-DjanView Answer on Stackoverflow
Solution 2 - IphonedjromeroView Answer on Stackoverflow
Solution 3 - IphoneDietrich EppView Answer on Stackoverflow
Solution 4 - IphoneTaumView Answer on Stackoverflow
Solution 5 - IphonekmkaplanView Answer on Stackoverflow
Solution 6 - IphonewirrwarrView Answer on Stackoverflow
Solution 7 - IphoneborcheroView Answer on Stackoverflow
Solution 8 - IphoneAndrey ChernukhaView Answer on Stackoverflow
Solution 9 - IphoneSteve RogersView Answer on Stackoverflow
Solution 10 - IphoneAlessandro VendruscoloView Answer on Stackoverflow
Solution 11 - IphonejhelzerView Answer on Stackoverflow
Solution 12 - IphoneSaliomView Answer on Stackoverflow
Solution 13 - IphoneVytautasView Answer on Stackoverflow
Solution 14 - IphoneRamy Al ZuhouriView Answer on Stackoverflow
Solution 15 - IphoneDuyen-HoaView Answer on Stackoverflow
Solution 16 - IphoneRaymondView Answer on Stackoverflow
Solution 17 - IphoneRajesh LoganathanView Answer on Stackoverflow
Solution 18 - IphoneMutaweView Answer on Stackoverflow