- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 200字
- 2021-07-02 14:45:04
Dangling pointers
Dangling pointers are the opposite of over-retained objects. You can encounter dangling pointers when trying to access a deallocated object. This is best illustrated by the following example:
- (void) doSomething {
MyObject *myObject = [[MyObject alloc] init]; // retainCount is +1
[myObject release]; // retain count is 0; object is deallocated
// More things happening in this method
[myObject myMethod];
}
This may not cause a crash all of the time, as myObject could be properly set to nil. When it crashes, you'll face the dreaded NSInvalidArgumentException, which, upon first glance, doesn't make much sense:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[<insert a type here> myMethod]: unrecognized selector sent to instance 0x12345679
We never have a call to myMethod on any other type. This crash often means that the contents of the memory at the myObject address have been replaced by some other object. Because we called release too early, the memory has been reclaimed, and potentially replaced by another object.
In order to overcome the burden of writing the manual reference counting, Apple introduced ARC. In the next section, we'll look at what this compiler feature can do, in order to provide efficient memory management.
- 數(shù)據(jù)庫基礎(chǔ)教程(SQL Server平臺)
- 大數(shù)據(jù)技術(shù)基礎(chǔ)
- 算法競賽入門經(jīng)典:習(xí)題與解答
- Access 2007數(shù)據(jù)庫應(yīng)用上機指導(dǎo)與練習(xí)
- 數(shù)據(jù)庫應(yīng)用基礎(chǔ)教程(Visual FoxPro 9.0)
- 算法與數(shù)據(jù)中臺:基于Google、Facebook與微博實踐
- 數(shù)據(jù)驅(qū)動:從方法到實踐
- 一個64位操作系統(tǒng)的設(shè)計與實現(xiàn)
- 深入淺出 Hyperscan:高性能正則表達式算法原理與設(shè)計
- 數(shù)據(jù)科學(xué)實戰(zhàn)指南
- 數(shù)據(jù)庫應(yīng)用系統(tǒng)開發(fā)實例
- Augmented Reality using Appcelerator Titanium Starter
- MySQL DBA修煉之道
- 大數(shù)據(jù)時代系列(套裝9冊)
- 數(shù)據(jù)指標體系:構(gòu)建方法與應(yīng)用實踐