Why Does the Following Code Crash on an iPhone 5 But Not an iPhone 5S

Why does the following code crash on an iPhone 5 but not an iPhone 5S?

The Int integer type is a 32-bit integer on the iPhone 5 and a 64-bit integer on the 5S. Since arc4random() returns a UInt32, which has twice the positive range of an Int on the iPhone 5, your first version basically has a 50% chance of crashing on this line:

var index = Int(arc4random())

Your modified version waits to convert until you take the modulo sum with max, so it's safe to convert to Int there. You should check out arc4random_uniform, which handles the modulo for you and avoids some bias inherent in your current implementation.

Why does this random function code crash on an iPhone 5 and 5S.

arc4random() yields an unsigned 32-bit integer (UInt32). The standard Int corresponds to 32-bit (signed) integer on iPhone 5 (iPhone 5 - 1.3 GHz dual core 32-bit ARMv7-A processor), Int32 type, in which case Int(arc4random()) will yield a(n) (integer overflow) runtime exception ~50% of the time the line above runs, on average. Why? Half of the numbers representable by UInt32 type are too large to be represented by the Int32 type.

print(INT32_MAX)  // 2147483647
print(UINT32_MAX) // 4294967295 <-- max return of arc4random()

There is really no need for you to generate any random number in the range of a 32-bit unsigned integer using the arc4random() function; an alternative is using arc4random_uniform() to generate a random number in the range 0..<30. Try replacing the line you've marked for error with the following:

let randomImageSelection = arc4random_uniform(UInt32(imageArray.count))
/* random Integer in: 0 ..< imageArray.count */

Closely related thread (to which this is possible a duplicate), thanks @Eric D.

  • App Crash iPhone 5 and below

App crashes on iPhone 5S but not on iPhone 4S.


  1. A good start will be to integrate a crash log collecting, crash reporting SDK in your app like HockeyApp, TestFlight, Crittercism or QuincyKit.

    This will send you the crash logs to you so you don't have to collect them manually. These tools can also automatically symbolicate the crash logs for you so you can find the source of the crash in your code.

  2. You should test a release build before you ship it. Archive a release build and distribute it as an Ad-Hoc build. You can load a saved IPA file to your iDevice using iTunes or Organizer. The thing is that you can test a release build which is the same you would ship to the App Store (they are signed differently but the build configuration is the same unless you changed that).

  3. iPhone 5S ships with 64-bit A7 processor. Your iPhone 4 is a 32-bit device. Most likely processor architecture is not the case but as Apple says: Before you distribute your app, you must test it on actual hardware. Some of the runtime changes can be detected only when the app is running on a device. I recommend you updating your set of test devices with different models. I still own an old iPhone 3GS which is great for spotting performance issues.

Memcpy crash only on iPhone 5s

This is crashing because you are using int not because of memcpy. Try replacing the int with NSInteger and it should work. You can find more information here.

The iPhone5S uses a 64bit architecture while the others use 32bit. One of the core differences between the two is how the system handles integers as explained in the doc. Your code is using unsigned int which does not mean the same thing on 32bit and on 64bit and that results in a crash. You should change your int called i and j to NSIntegers and I believe your code will work. Or come to think of it you can try to simply replace the word int by long.

App Crash iPhone 5 and below

The maximum value of a 32 bits integer is 2 147 483 647.

Your delay value may be greater than that, so sometimes it will crash on 32 bits platforms like these devices you listed because Int won't be able to hold the value.

App Crash iPhone 5 and below

The maximum value of a 32 bits integer is 2 147 483 647.

Your delay value may be greater than that, so sometimes it will crash on 32 bits platforms like these devices you listed because Int won't be able to hold the value.



Related Topics



Leave a reply



Submit