Uploaded image for project: 'Swift'
  1. Swift
  2. SR-6645

EXC_BAD_ACCESS in code using protocol extension for defining default implementation

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Duplicate
    • Component/s: Compiler
    • Labels:
    • Environment:

      XCode 9.2 (9C40b), Swift 4, MacOS Sierra 10.12.6

      Description

      The following playground sample results in a crash (EXC_BAD_ACCESS) while it should work perfectly fine as far as I can tell:
       

      public protocol Observable: class {
          associatedtype Observer: AnyObject
          var observers: [Observer] { get set }
          func addObserver(_ observer: Observer) -> Void
      }
      
      public extension Observable {
          public func addObserver(_ observer: Observer) -> Void {
              observers.append(observer)
          }
      }
      
      protocol TestObservableListener: class {
          func bla()
      }
      
      protocol TestObservable: Observable where Observer == TestObservableListener {
      }
      
      class TestObservableImpl: TestObservable {
          typealias Observer = TestObservableListener
          public var observers = [Observer]()
          func broadcast() {
              for observer in observers {
                  //2
                  observer.bla()
              }
          }
      }
      
      class TestObserverImpl: TestObservableListener {
          func bla() {
              print("bla")
          }
      }
      
      let observable = TestObservableImpl()
      let observer = TestObserverImpl()
      observable.addObserver(observer)
      //1
      observable.broadcast()
      

      It crashes on line marked with 2, when called from 1.

      It seems to occur because a protocol extension is defined with a default implementation for the addObserver method.

      The full crash:

      error: Playground execution aborted: error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0xffff9f7ffffa0440).
      The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.

       See also: https://stackoverflow.com/questions/47901032/swift-exc-bad-access-with-default-implementation-defined-in-protocol-extension

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              wernera77 Werner Altewischer
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: