官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 海安县| 安福县| 定南县| 慈溪市| 温宿县| 龙陵县| 江城| 滕州市| 汉源县| 页游| 工布江达县| 宾川县| 铜梁县| 瑞昌市| 皋兰县| 沾益县| 白玉县| 探索| 刚察县| 金沙县| 凤城市| 贵定县| 平江县| 卢氏县| 将乐县| 禹城市| 西乌珠穆沁旗| 丹寨县| 南丹县| 莎车县| 惠安县| 德惠市| 册亨县| 赤城县| 龙海市| 分宜县| 应用必备| 铁力市| 葵青区| 克山县| 宽甸|