- 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.
推薦閱讀
- 數(shù)據(jù)挖掘原理與實(shí)踐
- Unity 5.x Game AI Programming Cookbook
- Developing Mobile Games with Moai SDK
- UDK iOS Game Development Beginner's Guide
- 深入淺出MySQL:數(shù)據(jù)庫開發(fā)、優(yōu)化與管理維護(hù)(第2版)
- Hadoop 3.x大數(shù)據(jù)開發(fā)實(shí)戰(zhàn)
- 大數(shù)據(jù)架構(gòu)商業(yè)之路:從業(yè)務(wù)需求到技術(shù)方案
- PostgreSQL指南:內(nèi)幕探索
- 辦公應(yīng)用與計(jì)算思維案例教程
- TextMate How-to
- 大數(shù)據(jù)分析:數(shù)據(jù)倉庫項(xiàng)目實(shí)戰(zhàn)
- Expert Python Programming(Third Edition)
- 商業(yè)智能工具應(yīng)用與數(shù)據(jù)可視化
- 智能與數(shù)據(jù)重構(gòu)世界
- Unity for Architectural Visualization