-
Notifications
You must be signed in to change notification settings - Fork 68
Description
iOS Crash, I think it is caused by memory management error when using third party library NSObject+Rx and Then?
Could you give me some tips?
Firebase crashlytics reports,
已崩溃:com.apple.main-thread
EXC_BREAKPOINT 0x0000000104a5be14
0
IOS
LoginRegisterInputView.swift - 第 328 行
closure #5 in LoginRegisterInputView.initRegisterUI()
1
Then
$S4ThenAAPAARlzCrlE4thenyxyxKXEKF + 32
2
IOS
LoginRegisterInputView.swift - 第 314 行
LoginRegisterInputView.initRegisterUI()
3
IOS
LoginRegisterInputView.swift - 第 225 行
LoginRegisterInputView.initUI(style:)
4
IOS
LoginRegisterInputView.swift - 第 42 行
specialized LoginRegisterInputView.init(style:rootVC:)
Here is the code:
let registerProtocolLabel = UILabel().then {
let string = "RegisterIterm".localized().replacingOccurrences(of: "<a>", with: "+").replacingOccurrences(of: "</a>", with: "+")
let array:[String] = string.components(separatedBy: "+")
$0.font = mThemeMinFont
if array.count > 0 {
$0.attributedText = (array[0].color(mRGBA(120, 120, 120, 1)) + array[1].color(mThemePinkColor).underline + array[2].color(mRGBA(120, 120, 120, 1))).attributedText
}
$0.textAlignment = .left
$0.numberOfLines = 0
$0.lineBreakMode = .byWordWrapping
$0.rx.tapGesture().when(.recognized)
.subscribe(onNext:{ _ in
AppUtils.jumpToWebView(route: AppUtils.getFunctionUrl(.term))
}).disposed(by: rx.disposeBag)
}
The code is of bugs and nasty. It is not written by me, not by my colleague.
Now I am in charge of the bugs.
Reading from then, it is very easy.
I have a crash question, And I asked in [StackOverFlow](iOS Crash, memory management error when using NSObject+Rx and Then?)
The object just set its properties in the closure. It does not visit other properties. So it is very ARC, it will not interfere other objects' memory management.
While my colleague's code is not of Then, it did two much.
I think the crash is caused by NSObject+Rx.
NSObject+Rx's code is easy, just code sugar.
/// a unique DisposeBag that is related to the Reactive.Base instance only for Reference type
public var disposeBag: DisposeBag {
get {
return synchronizedBag {
if let disposeObject = objc_getAssociatedObject(base, &disposeBagContext) as? DisposeBag {
return disposeObject
}
let disposeObject = DisposeBag()
objc_setAssociatedObject(base, &disposeBagContext, disposeObject, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return disposeObject
}
}
Reading from the source code of NSObject+Rx, in the scene above ,I think
rx.disposeBag is equal to label.rx.disposeBag.
It may cause bugs.
RxSwift's source code is a little hard to read.
So what is the crash reason?