項目如何判斷是用MRC還是ARC呢播揪?
如何讓項目支持MRC或者支持ARC?
給文件添加flags -fobjc-arc 就能讓原來的MRC支持ARC
如果要禁止ARC,則將flags 改為 -fno-objc-arc
MRC
創(chuàng)建對象的幾種方式 alloc栅盲、new 、copy废恋、mutableCopy谈秫,這四種方法創(chuàng)建的對象需要自己發(fā)送release消息手動釋放扒寄。
要想持有一個對象就要調(diào)用retain方法。
方法的返回值拟烫,這種不知道什么時候該釋放的對象需要調(diào)用autorelease方法旗们,讓自動釋放池來管理對象的釋放。
ARC
基本不需要關(guān)系對象的創(chuàng)建和釋放构灸,編譯器會自動添加retain、release岸梨、autorelease方法喜颁。但有些對象也是需要做內(nèi)存的管理的,例如CoreFoundation產(chǎn)生的對象曹阔。做pdf轉(zhuǎn)圖片保存到相冊的時候遇到過的CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)[NSURL URLWithString:self.dataArr[_currentCount]]);
因為CGPDFDocumentCreateWithURL會retain創(chuàng)建的對象返回給pdf半开,而pdf又不是Objective-C對象,所以需要手動釋放赃份。CGPDFDocumentRelease(pdf);
寂拆,還有些內(nèi)容
NSTimer CADisplayLink 這兩個類在addTarget的時候會強引用target對象,很容易造成循環(huán)引用抓韩。
參考資料
http://www.lanrenios.com/tutorials/all/2012/1024/221.html
http://blog.csdn.net/wbw1985/article/details/7644815
ARC閱讀資料
https://developer.apple.com/library/content/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html
http://clang.llvm.org/docs/AutomaticReferenceCounting.html
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html#//apple_ref/doc/uid/10000011-SW1
http://clang.llvm.org/get_started.html
Clang 介紹
http://llvm.org/svn/llvm-project/cfe/trunk/
Runtime介紹
http://llvm.org/svn/llvm-project/cfe/trunk/lib/CodeGen/CGObjCMac.cpp
蘋果開源代碼 runtime庫實現(xiàn)
https://opensource.apple.com/source/objc4/objc4-493.9/
runtime/objc-arr.mm ARC相關(guān)的API
runtime/objc-weak.mm __weak修飾符功能API
libobjc2 是 Objective-C運行時庫的實現(xiàn)纠永,該庫通過GNUstep工程實現(xiàn)ARC
相當(dāng)于蘋果公司的Objective-C運行時庫的實現(xiàn) objc4
http://gnustep.blogspot.com/2011/07/gnustep-objective-c-runtime-15-released.html
http://thread.gmane.org/gmane.comp.lib.gnustep.general/36358
http://svn.gna.org/viewcvs/gnustep/libs/libobjc2/1.5/