Swift:谁可以帮助找出代码中导致内存泄漏的原因

时间:2016-03-04 05:58:23

标签: swift memory-leaks swift2 automatic-ref-counting

我正在制作演示,但我发现当我从相机拍照时内存会增加。所以一定有东西导致内存泄漏。我怎样才能检查它在哪里?

以下是我写的示例代码:

https://github.com/AarioAi/NotesOpen/tree/master/Swift/10.1.1%20Photo%20-%20Avatar

效果视频:

https://www.facebook.com/AarioAi/videos/vb.100011236983846/179251062459459/?type=2&theater

1 个答案:

答案 0 :(得分:1)

class Counsel {
    let bias:String = "Bias"
    var scale: Void -> String? = {
        return "Scale bias"
    }
    deinit {
        print("deinit")
    }
}

do {
    var counsel:Counsel? = Counsel()
    counsel = nil
}

按照预期在操场上打印deinit。看,deinit被称为

class Counsel {
    let bias:String = "Bias"
    var scale: Void -> String? = {
        return "Scale bias"
    }
    deinit {
        print("deinit")
    }
}

do {
    var counsel:Counsel? = Counsel()
    counsel = nil
    print("still in scope")
}

/*
deinit
still in scope
*/