App crashes in Release build but not in debug App crashes in Release build but not in debug ios ios

App crashes in Release build but not in debug

There are many reasons that an app might crash in release mode but not in debug mode (e.g. memory allocation differences showing up a bug that actually exists in both builds.) They can take a lot of work to track down, even with a non-beta compiler/language.

You say that the problem goes away if you do as I suggested and build for release with optimisations turned off. Given that the Swift compiler is still in beta and definitely still has the occasional problem—I've seen the compiler simply crash when building optimised builds—this may actually be an optimiser bug.

For now, therefore, I'd defer looking into it. Release without optimisations until we get a full release version of the compiler. Then, turn optimisations back on and see if you still have the problem. If you do, that's the time to start spending your energy trying to figure out if it's a compiler bug or a bug in your own code.

I've had the same problem. I finally fixed it by turning on whole module optimization. Combined with correct implementations of access controlthis should fix your crash.

Whole module optimization according to Apple:

Use Whole Module Optimization to infer final on internal declarations. Declarations with internal access (the default if nothing is declared) are only visible within the module where they are declared. Because Swift normally compiles the files that make up a module separately, the compiler cannot ascertain whether or not an internal declaration is overridden in a different file. However, if Whole Module Optimization is enabled, all of the module is compiled together at the same time. This allows the compiler to make inferences about the entire module together and infer final on declarations with internal if there are no visible overrides.

You can enable this in your project settings:

Whole Module Optimization

But be aware this option optimizes all of the files in a target together and enables better performance at the cost of increased compile time.

To catch the crash test the with the Optimization Level set to Fastest, Smallest [-Os] in Debug mode to more closely simulate the code that will be generated & running on the user’s device.

You can set it in build settings, under Swift Compiler/Code Generation