AttributedString icon indicating copy to clipboard operation
AttributedString copied to clipboard

iOS11以下崩溃

Open IAFung opened this issue 5 years ago • 3 comments

在iOS11以下的系统,kvo的监听会崩溃,An instance 0x7f9395d49750 of class SXLabel was deallocated while key value observers were still registered with it. Current observation info: 使用了textView的attachment attributed.text = .init("\(.view(customLabel, .original(.center))) \(string)", .paragraph(.lineSpacing(7)))

IAFung avatar Jan 04 '21 07:01 IAFung

我换电脑了 现在没有iOS11以下的环境, 能发一些详细的截图吗? 这个问题可以稳定复现吗?

lixiang1994 avatar Jan 06 '21 02:01 lixiang1994

稳定复现,使用kvo的keypath的方式在iOS11以下好像要手动调用invalidate释放 调用代码

class SXTextView: UITextView {
    private let customLabel = UILabel()
    @objc var string: String? {
        didSet {
            customLabel.text = "交易备注"
            customLabel.font = .systemFont(ofSize: 12)
            customLabel.backgroundColor = #colorLiteral(red: 0.9098039216, green: 0.2078431373, blue: 0.1333333333, alpha: 0.1004120291)
            customLabel.textColor = #colorLiteral(red: 0.9098039216, green: 0.2078431373, blue: 0.1333333333, alpha: 1)
            customLabel.textAlignment = .center
            customLabel.frame = CGRect(x: 0, y: -3, width: 58, height: 23)
            isEditable = false
            isSelectable = false
            attributed.text = .init("\(.view(customLabel, .original(.center)))    \(string!)", .paragraph(.lineSpacing(7)))
            attributed.layout()
        }
    }
    @objc func layout() {
        attributed.layout()
    }
}
let textView = SXTextView()
textView.string = "qeqewqkqwkjrqwjrkjqjnsdnfajsnfieuwrhweirhiuweqweqwe"
 view.addSubview(textView)

每次调用的页面释放时,崩溃信息如下:

2021-01-06 10:36:55.640 XibTest[14529:1236068] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x7ffa900ffe00 of class XibTest.SXTextView was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x618000039c20> (
<NSKeyValueObservance 0x61800005a190: Observer: 0x618000059d40, Key path: bounds, Options: <New: YES, Old: YES, Prior: NO> Context: 0x0, Property: 0x618000059830>
<NSKeyValueObservance 0x61800005a4c0: Observer: 0x61800005a520, Key path: frame, Options: <New: YES, Old: YES, Prior: NO> Context: 0x0, Property: 0x61800005a580>
)'

IAFung avatar Jan 06 '21 02:01 IAFung

你好, 还存在这个问题吗? 抱歉 过这么久才看到你的回复

lixiang1994 avatar Apr 28 '22 09:04 lixiang1994