5

When I run the following code (a simplified example, created to demonstrate the crash), it executes as expected when I choose Run (both os_log messages print in Console).

However, when I open it in Instruments from the Memory Debug Navigator - by pressing Restart - it crashes (only the first os_log message prints in Console).

The crash occurs at observe(...).

import os
import UIKit

class ObserverCrashingExample: NSObject {

    @objc private var animation: UIViewPropertyAnimator?
    private var observer: NSKeyValueObservation?

    override init() {

        super.init()

        animation = UIViewPropertyAnimator( duration: 1, curve: .linear, animations: { })

        animation!.pauseAnimation()

        os_log("X_AMPLE Executes")

        observer = animation!.observe(\.isRunning, options: [.new, .old]) { _ , _ in }

        os_log("X_AMPLE Does not execute")

    }

    required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }

}

I am running Xcode 9.3, Swift 4.1, iOS 11.3 deployment target.

Any ideas? Is this a compiler issue?

Cheers

Mark Jarecki
  • 115
  • 1
  • 13
  • It appears that this is an acknowledged bug. Hopefully, there'll be progress on this soon, as it is a deal-breaker for certain design patterns in Swift 4.1. – Mark Jarecki Apr 14 '18 at 10:26
  • Could you point me to where this is acknowledged as a bug? It looks like I can't profile my code any longer as well, I am also running Xcode 9.3 and Swift 4.1, but I can't pin-point an exact problematic place in my code yet... – NeverwinterMoon Apr 23 '18 at 14:07
  • That information is unfortunately not publicly available. Submit a bug report to Apple and they might give you more details. Generally, they will simply acknowledge if engineering has accepted the bug and has assigned it an ID. All you can hope for is Apple fixing it in the next update. – Mark Jarecki Apr 23 '18 at 18:29
  • 1
    In "Recording Options" (Alt-CMD-R) Turn off "Record reference counts" and "Enable Zombie detection" – Yohst May 07 '18 at 23:51
  • Thanks, Yohst. However, your suggestion does not work for me. The original bug is still open according to Apple. – Mark Jarecki May 11 '18 at 10:15
  • Tried Xcode 9.4. The problem isn't solved. – CopperCash Jun 07 '18 at 09:42
  • I know. It's frustrating. And points to more serious issues in the libraries. Sadly, Apple gives little to no information on when to expect a fix - only an issue ID and acknowledgement. Our work around, for the time being, is to adopt different patterns and avoid this altogether. – Mark Jarecki Jun 07 '18 at 10:10
  • So frustrating. Is Xcode 9.3 + KVO + Instruments a rare combination? I am not seeming a lot of complains on the Internet. – CopperCash Jun 08 '18 at 00:56
  • I agree, it’s incredibly frustrating, because I did not have these issues using earlier versions of Swift & XCode. I have seen some posts here and there, but I’m also surprised there is little outrage that the pattern is not sufficiently popular to be causing more widespread problems. I suspect many hold off being on the bleeding edge, especially with a language in flux. – Mark Jarecki Jun 08 '18 at 07:13

1 Answers1

0

I tried three word-arounds:

  1. Disabling "Record Reference Count". This stopped it from crashing. But the instruments showed me much more leaks than usual, which makes locating real leaks impossible.
  2. Commenting all my KVO calls. Didn't work in my case.
  3. Downgrading my code to Swift 4.0. I re-installed Xcode 9.2, and it works now.
CopperCash
  • 563
  • 5
  • 15