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-5752] KeyPath observations not torn down when nilled out #3814

Open
CharlesJS opened this issue Aug 23, 2017 · 2 comments
Open

[SR-5752] KeyPath observations not torn down when nilled out #3814

CharlesJS opened this issue Aug 23, 2017 · 2 comments

Comments

@CharlesJS
Copy link

Previous ID SR-5752
Radar rdar://34038087
Original Reporter @CharlesJS
Type Bug
Environment

Swift version 4.1-dev (LLVM 260a9efcdb, Clang b58a3e2689, Swift 5037e07171)

macOS 10.12.6 (16G29)

Additional Detail from JIRA
Votes 4
Component/s Foundation
Labels Bug
Assignee geza.miklo (JIRA)
Priority Medium

md5: 42af7aecb7147cf61ba7e61216a1c14a

Issue Description:

According to the WWDC video for the new Foundation block-based KeyPath observation mechanism, when the NSKeyValueObservation returned by observe() is nilled out, the observation should be torn down:

https://developer.apple.com/videos/play/wwdc2017/212/

However, this does not seem to be the case in practice, as you can see by running this program:

import Foundation

class C: NSObject {
    @objc dynamic var foo: String = "Foo"

    var observer: NSKeyValueObservation?

    override init() {
        super.init()

        self.observer = self.observe(\.foo) { s, _ in
            print("foo changed: \(s.foo)")
        }
    }

    deinit {
        print("deinit was called")
        self.observer?.invalidate()
        self.observer = nil
    }
}

do {
    let c = C()

    c.foo = "Bar"
}

When compiled and run, this program outputs:

foo changed: Bar
deinit was called
2017-08-23 12:22:01.950 Untitled[85112:12623133] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x7fff2cc09840 of class Untitled.C was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x7fff2cc0d8f0> (
<NSKeyValueObservance 0x7fff2cc0f7b0: Observer: 0x7fff2cc0bdc0, Key path: foo, Options: <New: NO, Old: NO, Prior: NO> Context: 0x0, Property: 0x7fff2cc0d820>
)'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff9ffde2cb __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fffb4e1d48d objc_exception_throw + 48
    2   CoreFoundation                      0x00007fffa005cc3d +[NSException raise:format:] + 205
    3   Foundation                          0x00007fffa19bc384 NSKVODeallocate + 293
    4   Untitled                            0x000000010225bc35 main + 133
    5   libdyld.dylib                       0x00007fffb5703235 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6

As you can see, the observation is still active, even after the observation token has both had .invalidate() called on it and been nilled out. This severely hampers the usefulness of the new observation functionality, since there doesn't appear to be any obvious way to use it without eventually crashing.

@swift-ci
Copy link
Contributor

swift-ci commented Nov 7, 2018

Comment by Ky (JIRA)

This is affecting me too, now. I'm trying to observe an NSRunningApplication's termination, but when it terminates this crash occurs because I don't get a chance to disconnect the observer; the observation block is never called

@swift-ci
Copy link
Contributor

swift-ci commented Oct 9, 2019

Comment by Géza Mikló (JIRA)

Still having this issue with Swift 5.1 vs iOS 10.3. The observers are stuck, cannot invalidate not remove them. There are still a large number of our users, using iOS 10

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants