Possible Bug in Swift Compiler's Optimizer

Sample Project URL: https://github.com/tobiasoleary/sample-ios-crash


I have an @IBDesignable CustomView in a Storyboard. When the CustomView's init?(coder:) method is called a crash occurs when I send the self reference to another class method in another class.


With Zombie's enabled I get the following message.

-[CompilerIssue.CustomView retain]: message sent to deallocated instance 0x7f8eaba46d10


This only occur in the standard Release build configuration and not the standard Debug build configuration.


I believe this may be a bug in the Swift Compiler, since if I change to the SWIFT_OPTIMIZATION_LEVEL from Fast [-O] to either [-Onone] or [-O -whole-module-optimization] I do not get this error.


However, it of course be something I'm doing wrong, since I've only been using Swift 2.0 for a couple of weeks.


What do you guys think?

Is there a better workaround then the one I provided in the WORKAROUND preprocessor flag?


@IBDesignable
class CustomView : UIView {

//...[SNIP]...

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        // Init Phase 1 Complete
        _setup()
    }

    private func _setup() {
        #if WORKAROUND
        let myself = self
        #endif
  
        redBox = UIView()
        blueBox = UIView()
  
        redBox.translatesAutoresizingMaskIntoConstraints = false
        blueBox.translatesAutoresizingMaskIntoConstraints = false
  
        redBox.backgroundColor = UIColor.redColor()
        blueBox.backgroundColor = UIColor.blueColor()
  
        let redBoxConstraints = [
            LayoutConstraintFactory.absoluteHeightConstraint(redBox, constant: 100.0),
            LayoutConstraintFactory.absoluteWidthConstraint(redBox, constant: 100.0)
        ]
        redBox.addConstraints(redBoxConstraints)
  
        let blueBoxConstraints = [
            LayoutConstraintFactory.absoluteHeightConstraint(blueBox, constant: 100.0),
            LayoutConstraintFactory.absoluteWidthConstraint(blueBox, constant: 100.0)
        ]
        blueBox.addConstraints(blueBoxConstraints)
  
        addSubview(redBox)
        addSubview(blueBox)
  
        let horizontalConstraints = [
            LayoutConstraintFactory.alignLeadingConstraint(redBox, toItem: self, constant: 8.0),  //<-- Crash Occurs Here
            LayoutConstraintFactory.afterConstraint(blueBox, toItem: redBox, constant: 8.0),
            LayoutConstraintFactory.alignTrailingConstraint(blueBox, toItem: self, constant: -8.0)
        ]
  
        let verticalConstraints = [
            LayoutConstraintFactory.alignTopConstaint(redBox, toItem: self, constant: 8.0),
            LayoutConstraintFactory.belowConstaint(blueBox, toItem: redBox, constant: 8.0),
            LayoutConstraintFactory.alignBottomConstaint(blueBox, toItem: self, constant:-8.0)
        ]
  
        addConstraints(horizontalConstraints + verticalConstraints)
  
        #if WORKAROUND
        let _ = myself.subviews.count > 0
        #endif

    }
//...[SNIP]...
}



class LayoutConstraintFactory {

    // ...[SNIP]...

    class func alignLeadingConstraint(item: UIView, toItem:UIView, constant: CGFloat = 0, multiplier: CGFloat = 1.0, relatedBy: NSLayoutRelation = .Equal) -> NSLayoutConstraint {
        return NSLayoutConstraint(item: item,
            attribute: NSLayoutAttribute.Leading,
            relatedBy: relatedBy,
            toItem: toItem,
            attribute: NSLayoutAttribute.Leading,
            multiplier: multiplier,
            constant: constant)
    }

    //...[SNIP]...
}

Accepted Reply

Hi totallyoleary,


Thank you for reporting this issue and for creating an Xcode project that reproduces the problem. I agree that this looks like a bug in the compiler optimizer.

If you file a bug report (radar) you will be notified when the problem is fixed.


Thanks,

Nadav

Replies

Known to affect XCode Version 7.0.1 (7A1001)


Can I mention if it affects pre-release versions of XCode here?

Hi totallyoleary,


Thank you for reporting this issue and for creating an Xcode project that reproduces the problem. I agree that this looks like a bug in the compiler optimizer.

If you file a bug report (radar) you will be notified when the problem is fixed.


Thanks,

Nadav

It's issue 23130439 in bugreport.apple.com. I found an XPC API Misuse thread that might be related.