- 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.
推薦閱讀
- Python數(shù)據(jù)分析與挖掘?qū)崙?zhàn)
- 大數(shù)據(jù)算法
- Ceph源碼分析
- 智能數(shù)據(jù)分析:入門、實戰(zhàn)與平臺構(gòu)建
- Starling Game Development Essentials
- MATLAB Graphics and Data Visualization Cookbook
- 新基建:數(shù)據(jù)中心創(chuàng)新之路
- Python數(shù)據(jù)分析與挖掘?qū)崙?zhàn)(第3版)
- 大數(shù)據(jù)治理與安全:從理論到開源實踐
- 探索新型智庫發(fā)展之路:藍迪國際智庫報告·2015(下冊)
- 區(qū)域云計算和大數(shù)據(jù)產(chǎn)業(yè)發(fā)展:浙江樣板
- 數(shù)據(jù)庫與數(shù)據(jù)處理:Access 2010實現(xiàn)
- Gideros Mobile Game Development
- R Machine Learning Essentials
- Mastering ROS for Robotics Programming(Second Edition)