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

Leaking with cycles

One common way to leak objects and their memory is to create strong references between different instances, and cut off any external references.

Let's consider the following code:

class MemoryLeak {
var ref: MemoryLeak?
init(ref: MemoryLeak) {
self.ref = ref
}

init() {
ref = self
}
}

As you can see, this is some code that you'd be unlikely to write on your own, as it doesn't have a purpose, aside from showcasing a memory leak.

The MemoryLeak class has two initializers, as follows:

  • One initializer can take a reference, on to which the instance will hold 
  • The other initializer creates a self reference

As you can imagine, this will not bode well if we use that class in code.

主站蜘蛛池模板: 理塘县| 县级市| 会昌县| 霍城县| 伽师县| 永昌县| 安阳县| 芜湖市| 吉林省| 英超| 老河口市| 延津县| 罗定市| 饶河县| 宁陕县| 洪湖市| 阿克苏市| 阳谷县| 阳新县| 政和县| 枞阳县| 吴堡县| 通许县| 察哈| 大英县| 定西市| 彩票| 克拉玛依市| 合作市| 文登市| 桑植县| 新民市| 石台县| 黔东| 鲁甸县| 明星| 库伦旗| 大竹县| 林口县| 出国| 屏山县|