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

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.

主站蜘蛛池模板: 石河子市| 平塘县| 余干县| 依兰县| 巴楚县| 裕民县| 益阳市| 奇台县| 乡城县| 岱山县| 昭苏县| 福建省| 大厂| 理塘县| 池州市| 丰台区| 米林县| 唐河县| 长汀县| 盐源县| 临夏市| 金山区| 靖宇县| 防城港市| 临漳县| 沂南县| 三台县| 阿克苏市| 昌江| 岳西县| 远安县| 贺兰县| 出国| 临城县| 林芝县| 新乡市| 建阳市| 清水河县| 青冈县| 韶山市| 中方县|