Asserts are hit in production build causing crashes
Asked Answered
D

1

10

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:

enter image description here

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?

Devitt answered 2/4, 2016 at 13:40 Comment(4)
Did you try my answer?Pathless
@Pathless No I did not. I thought it should not be required to add any custom flags to ensure it ignores asserts in release. I will try it now.Devitt
I also think it shouldn't be required (it was required in one of the first beta versions).Pathless
@Pathless Please see my edit - the default behaviour of assers is broken in Frameworks.Devitt
P
4

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.

Pathless answered 2/4, 2016 at 14:16 Comment(7)
Where do I put these flags? Which project, which target? I know about the bugs there. They are rare, minor and I plan to fix them in future releases. Crashes cause by asserts are much more severe.Devitt
@drasto You have to put them to the project & target that contains the asserts. It's just a compiler flag.Pathless
It does not seem to work - I put -assert-config Debug to project containing asserts (this is Framework project linked from another project), run it from XCode but the asserts are still hitDevitt
@drasto You should be putting Release there, not Debug.Pathless
I wanted to try if it makes any difference without having to archive, publish and download - Debug should disable assert during debug as well, right?Devitt
Oh sorry, I see. I think I understand now how that flag works. And it really disables asserts.Devitt
Do you know of any way how to disable assert for debug builds only? If I set -assert-config Release to Other Swift Flags asserts are disabled for both debug and release. If I just set it Other Swift Flags to -assert-config Release for Release (after expanding Other Swift Flags with arrow) and leave it empty for Debug the asserts are not disabled in release builds...Devitt

© 2022 - 2024 — McMap. All rights reserved.