whitesmith/WSTagsField

NSKVODeallocate crash

mohammadabushalhoob opened this issue · 2 comments

Which version of the WSTagsField are you using?

3.1

On which platform does the issue happen?

iOS11

Are you using Carthage?

no

Are you using Cocoapods?

yes

Which version of Xcode are you using?

Xcode 9.2
Build version 9C40b

What did you do?

i have crash with this log
Fatal Exception: NSInternalInconsistencyException
An instance 0x103185600 of class WSTagsField.WSTagsField was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x170632020> ( <NSKeyValueObservance 0x17065ede0: Observer: 0x170a71240, Key path: layer.bounds, Options: <New: YES, Old: YES, Prior: NO> Context: 0x0, Property: 0x1702859b0> )

@abufaeys Thanks! I'll look at it.

@abufaeys Well, since you're using iOS 11 and since that version invalidate all observers automatically, then I'm suspect that you're facing a retain cycle where the WSTagsField instance remains allocated and so the observer. So, for example, if you have something like:

cell.tagsField.onDidChangeHeightTo = { _, _ in
    tableView.beginUpdates()
    tableView.endUpdates()
}

^ This will cause a retain cycle because the tableView is holding the cells and, by assigning that closure, the tagsField will hold the tableView, nor the tableview nor the cells can be deallocated.

Fix the retain cycle by passing a weak reference of the tableView:

cell.tagsField.onDidChangeHeightTo = { [weak tableView] _, _ in
    tableView?.beginUpdates()
    tableView?.endUpdates()
}

Even so, I applied this patch to support observer invalidation in older versions while the field is being deallocated.