//假设Number为预定义的类
Number* num = [[Number alloc] init];
Number* num2 = [num retain];//此时引用记数+1,现为2
[num2 release]; //num2 释放对内存数据的所有权 引用记数-1,现为1;
[num release];//num释放对内存数据的所有权 引用记数-1,现为0;
[num add:1 and 2];//bug,此时内存已释放。
//autoreleasepool 的使用 在MRC管理模式下,我们摒弃以前的用法,NSAutoreleasePool对象的使用,新手段为@autoreleasepool
@autoreleasepool {
Number* num = [[Number alloc] init];
[num autorelease];//由autoreleasepool来管理其内存的释放
}
```
对与Objective-c中属性的标识符可以总结为:
`@property (nonatomic/atomic,retain/assign/copy, readonly/readwrite) Number* num;`
(1)nonatomic/atomic,表示该属性是否是对多线程安全的,是不是使用线程锁,默认为atomic,
(2)retain/assign/copy,是有关对该属性的内存管理的,
a、**assign ** is the default. In the setter that is created by @synthesize, the value willsimply be assigned to the attribute, don’t operate the retain count. Myunderstanding is that "assign" should be used for non-pointer attributes.
b、**retain** is needed when the attribute is a pointer to an object. The setter generated by@synthesize will retain (aka add a retain count) the object. You will need torelease the object when you are finished with it.
c、**copy** is needed when the object is mutable. Use this if you need the value of theobject as it is at this moment, and you don't want that value to reflect anychanges made by other owners of the object. You will need to release the objectwhen you are finished with it because you are retaining the copy.
(3)**readwrite /readonly** -"readwrite" is the default. When you @synthesize, both a getter and asetter will be created for you. If you use "readonly", no setter willbe created. Use it for a value you don't want to ever change after the instantiationof the object.
**2. Objective-c语言中的ARC(AutomaticReference Counting)**
在ARC中与内存管理有关的标识符,可以分为变量标识符和属性标识符,对于变量默认为__strong,而对于属性默认为unsafe_unretained。也存在autoreleasepool。
对于变量的标识符有:
(1) __strong,is the default. An object remains “alive” as long as there is a strong pointerto it.
(2) __weak,specifies a reference that does not keep the referenced object alive. A weakreference is set to nil when there are no strong references to the object.
(3)__unsafe_unretained,specifies a reference that does not keep the referenced object alive and is notset to nil when there are no strong references to the object. If the object itreferences is deallocated, the pointer is left dangling.
(4)__autoreleasing,is used to denote arguments that are passed by reference (id *) and areautoreleased on return,managedby Autoreleasepool.
对于变量标识符的用法:
`__strong Number* num = [[Number alloc]init];`
****
在ARC内存管理模式下,其属性的标识符存在以下几种:
对于Core Foundation与objective-cObject进行交换时,需要用到的ARC管理机制有:
(1) (__bridge_transfer) op or alternatively CFBridgingRelease(op) is used to consume a retain-count of a CFTypeRef while transferring it over to ARC. This could also be represented by
id someObj =(__bridge <NSType>) op;
CFRelease(op);
(2) (__bridge_retained) op or alternatively CFBridgingRetain(op) is used to hand an NSObject over to CF-land while giving it a +1 retain count. You should handle a CFTypeRef you create this way the same as you would handle a result of CFStringCreateCopy().This could also be represented by