- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 151字
- 2021-07-02 14:45:04
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.
推薦閱讀
- Developing Mobile Games with Moai SDK
- Oracle RAC 11g實戰(zhàn)指南
- 卷積神經(jīng)網(wǎng)絡的Python實現(xiàn)
- Libgdx Cross/platform Game Development Cookbook
- Lean Mobile App Development
- Hadoop 3.x大數(shù)據(jù)開發(fā)實戰(zhàn)
- Lego Mindstorms EV3 Essentials
- 白話大數(shù)據(jù)與機器學習
- 數(shù)據(jù)庫技術實用教程
- INSTANT Android Fragmentation Management How-to
- Hadoop大數(shù)據(jù)開發(fā)案例教程與項目實戰(zhàn)(在線實驗+在線自測)
- 區(qū)域云計算和大數(shù)據(jù)產(chǎn)業(yè)發(fā)展:浙江樣板
- 二進制分析實戰(zhàn)
- 活用數(shù)據(jù):驅(qū)動業(yè)務的數(shù)據(jù)分析實戰(zhàn)
- 區(qū)塊鏈+:落地場景與應用實戰(zhàn)