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

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.

主站蜘蛛池模板: 广河县| 渭南市| 泰和县| 清流县| 股票| 察雅县| 临湘市| 德格县| 上杭县| 望谟县| 台安县| 噶尔县| 平凉市| 繁峙县| 伊川县| 自贡市| 潞城市| 海宁市| 盘锦市| 上饶市| 溆浦县| 龙里县| 治多县| 彝良县| 大余县| 慈溪市| 精河县| 中宁县| 北安市| 吉木萨尔县| 子长县| 阳山县| 永泰县| 姜堰市| 泊头市| 浏阳市| 永定县| 安龙县| 宜兰县| 乾安县| 香港 |