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

Memory leaks

This issue is probably the most common in Objective-C and Swift. A memory leak occurs when an allocated object is not referenced anymore, but still has a retain count greater than 0. In Objective-C, those are very easy to write, as follows:

- (void) doSomething {
NSString *string = [[NSString alloc] init];

// do something with the string
[string retain];

// Do more things with the string
[string release];
}

Unlike the previous example, there's an additional call to retain, and this call will increment the retain count effectively. Every alloc and retain call should be balanced by release, and we failed to do so in the example. You now have a leak in your program. There are many ways to generate leaks in Swift, and in later sections of this book, we'll cover chapter how they can appear, how to debug them, and how to fix them.

主站蜘蛛池模板: 乐昌市| 桂阳县| 霍城县| 罗源县| 锦屏县| 息烽县| 八宿县| 兴海县| 凤冈县| 会昌县| 衡水市| 彰化市| 汉川市| 天门市| 新郑市| 祁门县| 卢龙县| 三都| 通海县| 辛集市| 永州市| 吉隆县| 巴林右旗| 正镶白旗| 仁布县| 错那县| 长春市| 中牟县| 泌阳县| 宜宾市| 平罗县| 江口县| 蒲城县| 武威市| 鹤庆县| 吴旗县| 靖西县| 岐山县| 吉安市| 乌什县| 从江县|