I'm trying to figure out why is a weak reference losing its referenced object even after the object keeps existing.
My code looks like:
MyClass {
deinit {
print("I'm being deinited") // This never gets called
}
func doConnection(connection: Future<Data, ServerConnectionError>) {
Future<Void, ServerConnectionError> { complete in
connection.onSuccess {[weak self] data in
guard let strongSelf = self else {
return // This line gets called
}
...
}
}
}
}
By inspecting the memory graph, I can see that the object referenced by self before the future completes is still alive (judging by the memory address).
Here's what the memory graph for the supposedly missing object looks like after finding its weak reference as nil (MyClass instance in far right):
The upper subtree is the one supposed to hold the object alive, while the bottom one has to do with the current stack execution. The object (blue box) that is referenced by many others in the 3rd level (from right to left) holds a strong reference to an array, which in turn holds a reference to the MyClass instance.
EDIT: Problem solved in answer below. Will mark as solved after cooldown passes.
Problem solved. Ok, so I don't know what was going on, but it fixed itself after multiple attempts to debug it. One thing that is still buggy is that the debugger show self
as nil
inside the closure (which added a significant time in the debugging process), but the guard let
block executes successfully and I get a strong reference to the object.