Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS Crash, memory management error #71

Open
BoxDengJZ opened this issue May 5, 2019 · 1 comment
Open

iOS Crash, memory management error #71

BoxDengJZ opened this issue May 5, 2019 · 1 comment

Comments

@BoxDengJZ
Copy link

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?

@ashfurrow
Copy link
Member

rx.disposeBag will never equal label.rx.disposeBag. If your code is crashing here and you suspect the use of Then, try removing it. The compiler will give you different diagnostic messages when used outside a closure used by Then.

If I had to guess, I would investigate the tap gesture recognizer firing when you don't expect it. I would try something like this:

$0.rx.tapGesture().when(.recognized).takeUntil(self.viewWillDisappear)

From this library. Good luck!

@BoxDengJZ BoxDengJZ reopened this May 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants