Swift 4 NSKeyValueObservation crash on iOS 10 and below

Originator:thongchaikol
Number:rdar://35027212 Date Originated:October 17 2017, 7:25 PM
Status: Resolved:
Product:iOS + SDK Product Version:Xcode Version 9.0 (9A235), iPad Air 2 - iOS 9.3 simulator
Classification:Bug Reproducible:Always
 
Area:
Xcode

Summary:
Inside a class, there's an NSKeyValueObservation object set up to observe an observable (@objc dynamic) object. If that class is deallocated, on iOS 10, 9 or 8 (at least), it crashes. I have created a sample project below, attached and github link.

Steps to Reproduce:
- clone this repo https://github.com/viki-org/swift4-kvo-crash-demo
- run on iOS 10 or 9 simulator
- click on red "Crash button"

Expected Results:
No crash

Actual Results:
See crash: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x7f8310624ed0 of class VKVideoSession was deallocated while key value observers were still registered with it. Current observation info:   (
  Context: 0x0, Property: 0x7f83102fa200>


Version/Build:
Xcode Version 9.0 (9A235), iPad Air 2 - iOS 9.3 simulator

Configuration:

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!