iOS-OC的MRC和ARC内存管理机制

1、 Objective-c语言中的MRCMannulReference Counting

  在MRC的内存管理模式下,对变量的管理相关的方法有:retain,releaseautoreleaseretainrelease方法操作的是引用记数器,当引用记数为零时,便自动释放内存。并且可以用NSAutoreleasePool对象,对加入自动释放池(autorelease调用)的变量进行管理,当drain时回收内存。

1retain,该方法的作用是将内存数据的所有权附给另一指针变量,引用数加1,即retainCount+= 1;

2release,该方法是释放指针变量对内存数据的所有权,引用数减1,即retainCount-= 1;

3autorelease,该方法是将该对象内存的管理放到autoreleasepool中。

2.Objective-c语言中的ARCAutomaticReference Counting

   在ARC中与内存管理有关的标识符,可以分为变量标识符和属性标识符,对于变量有__strong,__weak,默认是strong;而对于属性表识符有:

@property(nonatomic/atomic,assign/retain/strong/weak/unsafe_unretained/copy,readonly/readwrite) Number* num;//默认为unsafe_unretained

也存在autoreleasepool

 

***对于Core Foundationobjective-cObject进行交换时,需要用到的ARC管理机制有:

 

(1) (__bridge_transfer<NSType>) op oralternatively CFBridgingRelease(op) isused to consume a retain-count of a CFTypeRef whiletransferring it over to ARC. This could also be represented by id someObj =(__bridge <NSType>) op; CFRelease(op);

 

 

 

(2) (__bridge_retained<CFType>) op oralternatively CFBridgingRetain(op) isused to hand an NSObject overto CF-land while giving it a +1 retain count. You should handle a CFTypeRefyoucreate this way the same as you would handle a result of CFStringCreateCopy().This could also be represented by CFRetain((__bridge CFType)op); CFTypeRef someTypeRef =(__bridge CFType)op; 

 

(3) __bridge justcasts between pointer-land and Objective-C object-land. If you have noinclination to use the conversions above, use this one.

 

注意:Xcode4以前没有ARC。

原文地址:https://www.cnblogs.com/linxiu-0925/p/5030798.html