Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-8048] Computed variable setter inside another computed variable setter crashes the app #50581

Open
swift-ci opened this issue Jun 20, 2018 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-8048
Radar None
Original Reporter trololo (JIRA User)
Type Bug

Attachment: Download

Environment

xCode 9F1027a

Swift 4.1

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, CompilerCrash
Assignee None
Priority Medium

md5: 91900d0afc1ff3f92d5ede176d1ed547

Issue Description:

The code below works in the Playground but crashes in the iOS app.

SomeLoader().selectedIndex = 1

class SomeLoader {

// MARK: - Public

  var selectedIndex: Int? {
    get {
      return dataStorage.selectedIndex
    }
    set {
      dataStorage.selectedIndex = newValue
    }
  }
}

This code works:

SomeLoader().selectedIndex = 1

class SomeLoader {

// MARK: - Public

  var selectedIndex: Int? {
    get {
      return dataStorage.selectedIndex
    }
    set {
      dataStorage.updateSelected(index: newValue)
    }
  }
}

where:

struct DataStorage<T: Hashable> {
  enum Keys: String {
    case selectedIndex
  }
  private func get<U>(forKey key: String) -> U? {
    guard let objectData = getData(forKey: key) else {
      return nil
    }
    let object: U? = get(forData: objectData)
    return object
  }
  private func get<U>(forData objectData: Data) -> U? {
    return NSKeyedUnarchiver.unarchiveObject(with: objectData) as? U
  }
  private func save<U>(forKey key: String, object: U) {
    let encodedData = NSKeyedArchiver.archivedData(withRootObject: object)
    UserDefaults.standard.set(encodedData, forKey: key)
  }
}
extension DataStorage {
  func updateSelected(index: Int?) {
    guard let index = index else {
      remove(forKey: Keys.selectedIndex.rawValue)
      return
    }
    saveSelected(index: index)
  }
}

This is the callstack. The strange thing that the app crashes later.

@belkadan
Copy link
Contributor

I don't see a selectedIndex property in your DataStorage type. Can you attach the whole project?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software
Projects
None yet
Development

No branches or pull requests

3 participants