SCNView doesn't free added gesture recognizers after deinit

Originator:nazarof.anton
Number:rdar://6164844138987523 Date Originated:9 July, 2019
Status: Resolved:
Product:SceneKit Product Version:12.0
Classification:Bug Reproducible:Yes
 
CNView doesn't free added gesture recognizers after deinit. Please check out provided test case.

var wasDeallocated = false
class MyGestureRecognizer: UIGestureRecognizer {
    deinit { wasDeallocated = true }
}

class Foo {
    let scnView = SCNView() // test passes if change to `UIView()`

    init() {
        scnView.addGestureRecognizer(MyGestureRecognizer(target: self, action: #selector(test)))
    }

    @objc func test() { }
}

final class Test: XCTestCase {
    func testMemoryLeak() {
        autoreleasepool {
            _ = Foo()
        }
        XCTAssertTrue(wasDeallocated)
    }
}

Comments

TLDR: This is a bug, but there is not a recognizer leak, but a whole scene leak. Workaround: create another one SCNView instance.

  1. That run-time check could not verify the leak because according to the internal calls there can be a lot of autoreleases triggered by a specific run loop. Please, use the Instruments to verify memory leaks.
  2. What is more interesting is that I've checked this code in Instruments and the Foo was destructed, but the SCNView scene stays alive. So, your real problem is probably the whole scene, which keeps alive everything it's own.
  3. What is much more interesting: this leaked scene finally deallocated (gesture recognizer deallocated too, of course) when another SCNView instance created. Just like that – you create new SCNView instance and the old one dies. Treat it as a workaround.
By vladlexion at Sept. 14, 2019, 7:27 a.m. (reply...)

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!