Asserts are hit in production build causing crashes

☆樱花仙子☆ 提交于 2019-12-09 11:49:50

问题


I have several assert(condition, "message") statements in my project.

They are used to check invariant conditions during development. I thought they would be ignored in production/release build (as stated in this answer). They are not. Instead they cause crashes during TestFlight testing. When I comment asserts the app does not crash. Something usually gets wrong a bit but it does not crash.

Can it be something with my build settings?

All my archive schemes use release configuration:

The asserts are in Cocoa Touch Framework project, that is used from custom keyboard extension.

All the targets in all projects (Cocoa Touch Framework, and the main project with keyboard extension target) have these Build Settings:

Enable Foundation Assertions
    Debug    YES
    Release  NO

Disable Safety Checks  NO

What's wrong?


EDIT:

Sulthan's answer shows how to disable asserts globally for both debug and relase builds. That is not what I need. I want it to work as expected - asserts should be enabled in debug but disabled in release builds.

By default it works that way - and it also works that way in my main project. But it does not work for asserts located in Framework project that is linked from that main project (details in this question). Why? How to fix it?


回答1:


The options you have tried:

Enable Foundation Assertions is in the preprocessing section (Macros). Swift is not preprocessed and does not use macros. This option disables NSAssert, NSParameterAssert and similar macros commonly used in Objective-C.

Disable Safety Checks is a performance option:

By default, the standard library guarantees memory safety. Many functions and methods document the requirements that must be satisfied by the caller, such as an array index being valid; memory safety is guaranteed even if a requirement is violated. However, violating a requirement can trigger a runtime error. APIs that include the word “unsafe” in their name let you explicitly disable safety checks in places where you need the additional performance. It’s your responsibility to verify the memory safety of code that uses unsafe APIs. Memory safety is also not guaranteed if there is a race condition in multithreaded code.

(Swift Library Reference)

You should probably try my answer here (use -assert-config Release in Other Swift Flags).

Or just keep the asserts in production builds. Every failing assert is a bug and in general it's better to know about a bug as soon as possible.



来源:https://stackoverflow.com/questions/36374007/asserts-are-hit-in-production-build-causing-crashes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!