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

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.

主站蜘蛛池模板: 湖北省| 文登市| 内丘县| 盐池县| 钟祥市| 太谷县| 平远县| 应用必备| 汉川市| 白银市| 青岛市| 沙湾县| 怀宁县| 广元市| 弥渡县| 延川县| 丰原市| 讷河市| 佳木斯市| 义马市| 开封县| 德清县| 尤溪县| 泗阳县| 孙吴县| 五指山市| 郑州市| 泌阳县| 新绛县| 如东县| 阳高县| 泾川县| 平远县| 阿克| 巴东县| 固安县| 麻江县| 宜兴市| 蒲城县| 科技| 许昌市|