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

EXC_BAD_ACCESS in Swift when accessing @optional ObjC properties

    XMLWordPrintable

    Details

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

      Description

      Swift code can crash at runtime with EXC_BAD_ACCESS when you use a nonnull property as an @optional requirement in ObjC that mistakenly returns nil.

      This happens in debug and release builds with unguarded access to that property, and only in release if used in an if let x = obj.objcProperty.

      It only seems to occur when the property isn't a bridged type (it doesn't crash with String).

      @protocol SomeProtocol
      @optional
      @property (nonnull) NSString * someString;
      @property (nonnull) SomeSwiftType * someObject;
      @end
      

       

      @implementation TheBug (SomeProtocol)
      -(NSString *) someString { // Calling this works.
          return nil;
      }
      -(SomeSwiftType *) someObject { // Calling this crashes.
          return nil;
      }
      @end
      

       
      The crash output looks like this:

      Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
      

      See the attached project for a more nuanced example (ViewController.swift). Note that you will have to run on the debug or release schemes to see all effects.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              bnut Andrew Bennett
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: