EXC_BAD_ACCESS "tried to save a deallocated object" in `deinit`

The following code snippet fails deinit

with EXC_BAD_ACCESS and previously with the EXC_BREAKPOINT method in the method _swift_abortRetainUnowned

with the message " tried to save a freed object ".

I'm wondering why the imported one self

will be saved when written as unowned

- and even if the closure is declared explicitly as @noescape

. IMHO I shouldn't stay here when importing into the closure.

In contrast, when self

committed as a strong link, the code will succeed. I would understand this if it were the other way around. What am I missing?

import Foundation

public class Bar {

    private let _id = 1

    func foo(@noescape f: ()->()) -> () {
        f()
    }

    deinit {
        foo() { [unowned self] in
            let id = self._id
        }
    }    
}



func test() ->() {
    var cr = Bar()
}        
test()

      

Stack

UnownedSelf`_swift_abortRetainUnowned:
    0x100174d90 <+0>:  leaq   0x1979d(%rip), %rax       ; "attempted to retain deallocated object"
    0x100174d97 <+7>:  movq   %rax, 0x65ff2(%rip)       ; gCRAnnotations + 8
    0x100174d9e <+14>: int3   
->  0x100174d9f <+15>: nop    

      

+3


source to share





All Articles