- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 174字
- 2021-07-02 14:45:07
A simple leak
Now, let's write a function that creates an instance, and does nothing else:
func createLeak() {
let leak = MemoryLeak()
}
createLeak()
When we create the new MemoryLeak instance, a self reference is set, and the retain count will be two for the duration of the function call. When the function returns, the local leak variable is not referenced anymore, so the retain count is still one, and, if there are no references to this instance in the program, we will have a leak.
In a garbage collected language, this would never happen, as unreachable objects are deallocated automatically.
Now, let's use the memory graph tool to investigate this issue:

Using this tool, at a glance, we can see the following:
- Leaks are denoted with the purple issue icon
- Upon clicking on the leaking object, we can see the issue
- The dark arrows in the memory graph are for strong references
In one go, it is now really easy for you to identify what might be wrong with your programs.
推薦閱讀
- 計算機組成原理與接口技術:基于MIPS架構實驗教程(第2版)
- 有趣的二進制:軟件安全與逆向分析
- 從零開始學Hadoop大數據分析(視頻教學版)
- Hands-On Machine Learning with Microsoft Excel 2019
- Enterprise Integration with WSO2 ESB
- Lean Mobile App Development
- 數據庫系統原理及應用教程(第4版)
- 區塊鏈:看得見的信任
- Ceph源碼分析
- 數據庫技術實用教程
- 科研統計思維與方法:SPSS實戰
- Hadoop 3實戰指南
- SQL Server 2012實施與管理實戰指南
- Visual Studio 2012 and .NET 4.5 Expert Development Cookbook
- 算力芯片:高性能CPU/GPU/NPU微架構分析