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

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.

主站蜘蛛池模板: 黎城县| 昭通市| 恩施市| 宁强县| 平塘县| 阳东县| 丽水市| 凉山| 宣汉县| 万宁市| 沭阳县| 台北县| 胶南市| 德令哈市| 于都县| 思茅市| 无为县| 通山县| 纳雍县| 黄大仙区| 常熟市| 罗源县| 平昌县| 祁连县| 兴城市| 安徽省| 富锦市| 大城县| 锡林浩特市| 漳浦县| 金乡县| 潍坊市| 昆山市| 万全县| 东乌| 阿图什市| 五大连池市| 景德镇市| 东源县| 太仆寺旗| 南阳市|