[SR-1310] Referenced object of protocol type does not deallocate if its function throws #43918
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
compiler
The Swift compiler in itself
memory leak
bug: Memory leak
Attachment: Download
Environment
Swift 2.2
Additional Detail from JIRA
md5: f2cbcc5a14d3132082ee9fcc898fba52
Issue Description:
Class A implements a protocol -
protocol myProtocol {
func doThings() throws
}
Class B has the following property:
var a : myProtocol = A()
Class A implements the protocol:
func doThings() throws {
throw
}
Object let b = B() calls a's function.
let _ = try? a.doThings()
We then dealloc b, it's deinit method is called, but a's deinit method is never called.
There are three conditions for this to happen.
1.) a needs to be a property of protocol type on another class. If it's created the same way in function scope, a's deinit is called
2.) This happens only if doThings() throws. If it doesn't throw, a's deinit is called.
3.) This happens only if we run the function on a protocol type. If we run the function this way - try (a as! A).doThings(), a's deinit is called.
If you try to check the state of a after b has deinitialized, you can see it has become a "zombie optional".
The text was updated successfully, but these errors were encountered: