镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

iOS Crash, memory management error  #71

Open

Description

@BoxDengJZ

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?

Activity

  1. ashfurrow commented on May 6, 2019

    @ashfurrow
    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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions