swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.5k stars 10.35k forks source link

[SR-8748] Inline init crashes in release configuration #51257

Closed swift-ci closed 5 years ago

swift-ci commented 6 years ago
Previous ID SR-8748
Radar rdar://problem/44458427
Original Reporter grurenko (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment Xcode 9.4.1 (9F2000) Swift 4.1, Release configuration iPhone X iOS 11.4
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, OptimizedOnly, RunTimeCrash | |Assignee | None | |Priority | Medium | md5: eb8edd80a4f3d57f6f5cb37762d08d21

Issue Description:

What I do:

Build code below in Release configuration:

import UIKit

protocol Foo: class {
  func foo()
}

class Bar: Foo {
  func foo() {
    print("Success!")
  }
}

class Bazz: UIViewController {
  private let effectView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
  private let anyField = true
  unowned let bar: Foo

  init(_ foo: Foo) {
    bar = foo
    super.init(nibName: nil, bundle: nil)
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("")
  }
}

class Graph {
  let bar: Bar
  let bazz: Bazz
  let closure1: () -> Void
  let closure2: () -> Void

  init() {
    let bar = Bar()
    let bazz = Bazz(bar)
    closure1 = { [unowned bar] in
      bar.foo()
    }
    closure2 = { [unowned bar] in
      bar.foo()
    }
    self.bazz = bazz
    self.bar = bar
  }
}

func test() {
  let graph = Graph()
  graph.bazz.bar.foo()
}

What I expect:

Will print "Success!"

What I receive:

Fatal error: Attempted to read an unowned reference but object was already deallocated.

Possible workarounds:

1) Remove unowned from Bazz.bar property

2) Remove anyField property or chage type of effectView to UIView

3) Remove closure2 from Graph

belkadan commented 6 years ago

@swift-ci create

eeckstein commented 6 years ago

I could reproduce it with Xcode 9, but not anymore with Xcode 10. Seems to be fixed.
Can you please check with the latest Xcode 10 release?

swift-ci commented 5 years ago

Comment by Gleb Rurenko (JIRA)

It's not reproduced on Xcode 10.