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

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.

主站蜘蛛池模板: 修文县| 晋城| 海林市| 新邵县| 临朐县| 肇东市| 银川市| 保康县| 绥中县| 大化| 永寿县| 广元市| 河南省| 佛教| 罗江县| 庆城县| 扶余县| 万载县| 剑川县| 巴林右旗| 石首市| 永宁县| 吴忠市| 新和县| 东城区| 涟水县| 洪江市| 双流县| 南部县| 陕西省| 贡觉县| 习水县| 雷山县| 鄂温| 扎鲁特旗| 临潭县| 措美县| 平江县| 友谊县| 海安县| 南皮县|