- 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.
- 大數據技術基礎
- 有趣的二進制:軟件安全與逆向分析
- SQL入門經典(第5版)
- Developing Mobile Games with Moai SDK
- Modern Programming: Object Oriented Programming and Best Practices
- Learning Spring Boot
- 新型數據庫系統:原理、架構與實踐
- 分布式數據庫系統:大數據時代新型數據庫技術(第3版)
- 大數據時代下的智能轉型進程精選(套裝共10冊)
- ZeroMQ
- 重復數據刪除技術:面向大數據管理的縮減技術
- 大數據治理與安全:從理論到開源實踐
- 企業大數據處理:Spark、Druid、Flume與Kafka應用實踐
- 數據可視化五部曲
- Nagios Core Administrators Cookbook