Search code examples
crashios12

Application crash on ios12.0.1 when view remove from super


Date/Time: 2018-10-30 00:57:35 +0000 OS Version: iPhone OS 12.0.1 (16A404) Report Version: 104

Exception Type:  SIGSEGV
Exception Codes: SEGV_ACCERR at 0x10
Crashed Thread:  0

Thread 0 Crashed: 0 libobjc.A.dylib
0x00000001aa9f0d70 objc_msgSend + 16 1 Foundation
0x00000001ac3c7958 NSLayoutConstraintIsPotentiallyDanglyInContainer + 112 2 UIKitCore 0x00000001d8b78464 -[UIView+ 13431908 (AdditionalLayoutSupport) _snipDangliesWithForce:repairIfPossibleForViewThatMoved:newSuperview:oldSuperview:] + 424 3 UIKitCore 0x00000001d8b781c8 _UIViewRemoveConstraintsMadeDanglyByChangingSuperview + 1088 4 UIKitCore 0x00000001d8c19f54 __45-[UIView+ 14094164 (Hierarchy) _postMovedFromSuperview:]_block_invoke + 68 5
UIKitCore 0x00000001d8c19e74 -[UIView+ 14093940 (Hierarchy) _postMovedFromSuperview:] + 756 6 UIKitCore
0x00000001d8c17f38 __UIViewWasRemovedFromSuperview + 172 7 UIKitCore 0x00000001d8c17a18 -[UIView+ 14084632 (Hierarchy) removeFromSuperview] + 464

I have received some crash like this,only ios12.0.1


Solution

  • My reputation isn't high enough to comment, but I have experienced the same crash, which only started happening on iOS 12. I filed a bug report with Apple, so hopefully it gets resolved soon.

    Based on the stack trace it crashes when trying to cleanup dangling constraints, so one possible workaround you can try is removing all constraints before you remove the view.

    extension UIView {
        func removeAllConstraints() {
            let superViewConstraints = superview?.constraints.filter{ $0.firstItem === self || $0.secondItem === self } ?? []
    
            superview?.removeConstraints(superViewConstraints + constraints)
        }
    }
    

    ...and then before the view gets removed just call:

    view.removeAllConstraints() 
    

    I'm not 100% sure this will work because I have not been able to reproduce the crash myself, I've only seen it pop up on the crash analytics service I use.