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

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.

主站蜘蛛池模板: 峨眉山市| 定南县| 德州市| 玉屏| 神池县| 康平县| 北宁市| 仲巴县| 阳春市| 汝城县| 尤溪县| 喀喇沁旗| 临洮县| 田东县| 察雅县| 绥宁县| 邵武市| 皋兰县| 安泽县| 北流市| 司法| 张家港市| 开阳县| 陕西省| 乌苏市| 乌拉特后旗| 申扎县| 抚州市| 安西县| 精河县| 邯郸县| 嘉黎县| 乌兰县| 阳原县| 丹阳市| 乾安县| 仲巴县| 奉贤区| 深水埗区| 鹤山市| 南宫市|