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

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.

主站蜘蛛池模板: 阳春市| 通州市| 苏尼特左旗| 南陵县| 庄浪县| 民丰县| 河西区| 襄樊市| 兴安县| 都江堰市| 比如县| 通渭县| 都江堰市| 墨玉县| 思南县| 温州市| 女性| 迭部县| 巴中市| 青阳县| 遂宁市| 通城县| 万宁市| 勐海县| 华池县| 万源市| 喀什市| 芮城县| 德昌县| 周宁县| 子洲县| 日照市| 囊谦县| 靖安县| 县级市| 益阳市| 鹤壁市| 望谟县| 绥化市| 英山县| 利川市|