Closed JeniaTretiakova-ezlo closed 7 years ago
I do not understand item 3. If oldBCPeripheral
and newBCPeripheral
have same identifier will the CBPeripheral
delegate be dealloced for newBCPeripheral
? I do not see this since the reference to old CBPeripheral
will be held until the reassignment. Are you saying it is released?
oldBCPeripheral and newBCPeripheral is two different instance, BUT they both have the same reference to cbPeripheral. So newBCPeripheral sets itself like delegate for self.cbPeripheral, and after it oldBCPeripheral sets delegate to nil for the same instance.
deinit { self.cbPeripheral.delegate = nil } Peripheral.swift(line 207) as result newBCPeripheral has property self.cbPeripheral, but delegate for it has been set to nil.
I understand the issue now. I think this make be a better fix,
deinit {
if cbPeripheral.delegate === self {
cbPeripheral.delegate = nil
}
}
I will write a couple of new test cases to verify fix.
Good catch! Thanks.
I just pushed a fix for this to master. I will do a pod release this weekend.
if cbPeripheral.delegate === self { cbPeripheral.delegate = nil }
I think it should work, but I don't like how it seems. Instead of make delegate weak and lets manage memory with Automatic Reference Counting, you handle it yourself and added some extra code.
The CBPeripheralDelegate
is a property on CBPeripheral
I cannot change it.
There are wrong logic when called function loadRetrievedPeripheral().
self.cbPeripheral.delegate = self (line 203 in Peripheral.swift)
Way to fix: Peripheral.swift 1) remove deinit { self.cbPeripheral.delegate = nil } 2 ) and replace: var delegate: CBPeripheralDelegate? { get set } with: weak var delegate: CBPeripheralDelegate? { get set }