MRR:Manual retain-release
推荐:《iPhone/Mac Objective-C内存管理教程和原理剖析》
http://www.cnblogs.com/VinceYuan/archive/2010/03/08/1680488.html
- 生成的对象,有一个引用计数:retainCount,当retainCount = 0,内存就会被释放
- 用alloc/new/copy生成对象时,其 retainCount = 1
- retain 让retainCount + 1,release让retainCount - 1,成对出现:[obj retain] [obj release]
- 用autorelease能让retainCount的管理简单些,涉及到autorelease pool
- 在生成新的RunLoop时,系统自动创建新的autorelease pool
property的修饰符:
- assign
- copy
- retain:参考默认实现,很重要
- 如:@property (retain) objA* obj;
retain、copy的默认实现:
-(void) setObjB:(ClassB*) value
{
if (objB != value)
{
[objB
release]; //原来的对象会先release
objB = [value retain]; //copy类似
}
}
assign针对简单数据类型(scalar types) ,如int,float, CGRect, NSInteger等,它只是简单的赋值操作
copy/mutableCopy 浅拷贝和深拷贝
参考:《浅析ObjectiveC 深浅拷贝学习 》http://www.cocoachina.com/bbs/read.php?tid=80463
- 不可改变对象的copy,才是浅拷贝;其他都是深拷贝
- copy 返回的对象都是不可改变的
- 容器类(如NSArray等) 的内容都是浅拷贝的
内存泄露
参考:《Presentation: Finding & Fixing Mem Leaks》
http://www.streamingcolour.com/blog/2010/09/21/presentation-finding-fixing-mem-leaks/
常见情况1:
// @property (nonatomic, retain) MyClass* objA;
- (void)foo{
//retainCount = 2
self.objA = [[MyClass alloc] init];
}
- (void)dealloc{
//retainCount = 1
[objA release];
[super dealloc];
}
修复方法:
// @property (nonatomic, retain) MyClass* objA;
<!-- ^ Position is not set to relative / absolute here because of Mozilla -->
- (void)foo{
//retainCount = 1
MyClass *obj = [[MyClass alloc] init];
//retainCount = 2
self.objA = obj;
//retainCount = 1
[obj release]
}
- (void)dealloc{
//retainCount = 0
[objA release];
[super dealloc];
}
常见情况2:
<!-- ^ Position is not set to relative / absolute here because of Mozilla -->
// @property (nonatomic, retain) MyClass* objA;
- (void)foo{
MyClass *obj = [[MyClass alloc] init];
self.objA = obj;
[obj release];
//retainCount = 1
}
- (void)dealloc{
//retainCount = 1
[super dealloc];
}
<!-- ^ Position is not set to relative / absolute here because of Mozilla -->
修复方法:
// @property (nonatomic, retain) MyClass* objA;
- (void)foo{
MyClass *obj = [[MyClass alloc] init];
self.objA = obj;
[obj release];
//retainCount = 1
}
- (void)dealloc{
//retainCount = 0
[objA release];
[super dealloc];
}
self.abc = nil 与 _abc = nil的区别
@property (retain) classA *abc;
@synthesize abc = _abc;
需要明白 self.abc 的默认实现,它相当于:
[_abc release];
_abc = nil;
前者会发送release消息,而后者可能有内存泄露。
<!-- ^ Position is not set to relative / absolute here because of Mozilla -->
Tools
- Build & Analyze
- Leaks Instument
<!-- ^ Position is not set to relative / absolute here because of Mozilla -->
Automatic Reference Counting
- “Automatic Reference Counting (ARC) is a compiler-level feature that simplifies the process of managing object lifetimes (memory management) in Cocoa applications.”
- ARC is a pre-compilation step that adds retain/release/autorelease statements to your code for you
- 需要Xcode 4.2+, Apple LLVM 3.0+ compiler
<!-- ^ Position is not set to relative / absolute here because of Mozilla -->
简化了编程(本周上和MRR是一样的),不需要/不能使用下面这些方法
- NSAllocateObject / NSDeallocateObject
property的修饰符:
- strong:相当于retain
- weak:相当于assign
变量修饰符:
-
- __strong
- This means any object created usingalloc/init is retained for the lifetime of its current scope.
- is the default so you don’t need to type it.
- #相当于retain,在不再使用的时候被释放 (默认)
- __weak
- means the object can be destroyed at anytime.
- This is only useful if the object is somehow strongly referenced somewhere else.
- When destroyed, a variable with __weak is set to nil.
- #与assign很像,不同在于如果指向的数据被释放了,那么这个指向nil
- __unsafe_unretained
- is just like __weak but the poiner is not set to nil when the object is deallocated.
- Instead the pointer is left dangling (i.e. it no longer points to anything useful).
- #相当于assign,指向的数据如果被释放,这个指向原来的地址
- __autoreleasing
- not to be confused with calling autorelease on an object before returning it from a method, this is used for passing objects by reference, for example when passing NSError objects by reference such as [myObject performOperationWithError:&tmp];
- #标明传给函数的(id*)型参数是自动释放的,(函数中(id*)型参数默认的也是这种类型)
<!-- ^ Position is not set to relative / absolute here because of Mozilla -->
Toll-Free Bridging/CF和ObjC转换
-
在没有开启ARC时,可以进行强制转换.
- CFStringRef aCFString = (CFStringRef)aNSString;
- NSString *aNSString = (NSString *)aCFString;
- __bridge
-
简单赋值,不会影响两边对象的retain count.
id my_id;
CFStringRef my_cfref;
NSString *a = (__bridge NSString*)my_cfref;
CFStringRef b = (__bridge CFStringRef)my_id;
- __bridge_retained or CFBridgingRetain
- __bridge_transfer or CFBridgingRelease
-
例子
-
-(void)test
{
CFStringRef coreFoundationString = CFStringCreateWithCString(CFAllocatorGetDefault(),"C String", kCFStringEncodingUTF8); // 创建 retainCount = 1
id unknownObjectType = (__bridge id)coreFoundationString; // 简单赋值,不变,retainCount = 1
CFStringRef anotherString = (__bridge_retained CFStringRef)unknownObjectType; // 保留赋值,加一,retainCount = 2
NSString *objCString = (__bridge_transfer NSString *)coreFoundationString; // 释放赋值,减一,retainCount =1;由于NSString*默认strong,加一,retainCount = 2
NSLog(@"String = %@", objCString);
objCString = nil; // 不再指向原内存,原内存减一,retainCount = 1
CFRelease(anotherString); // 释放,减一,retainCount = 0
}
- 转换宏
-
#if __has_feature(objc_arc)
# define objc_retainedObject(o) ((__bridge_transfer id)(objc_objectptr_t)(o))
# define objc_unretainedObject(o) ((__bridge id)(objc_objectptr_t)(o))
# define objc_unretainedPointer(o) ((__bridge objc_objectptr_t)(id)(o))
#else
# define objc_retainedObject(o) ((id)(objc_objectptr_t)(o))
# define objc_unretainedObject(o) ((id)(objc_objectptr_t)(o))
# define objc_unretainedPointer(o) ((objc_objectptr_t)(id)(o))
#endif
@synthesize 和Dynamic ivars
@interface ClassA {
//NSObject _objB; // instance varible or ivar,可以不写,编译器会自动生成
}
@property (retain) NSObject *objB;
@end
@implement ClassA
@synthesize objB = _objB;
@end
<!-- ^ Position is not set to relative / absolute here because of Mozilla -->
@synthesize myProperty = myIvar; // a dynamic ivar named myIvar will be generated
@synthesize anotherProperty; // a dynamic ivar named anotherProperty will be generated
<!-- ^ Position is not set to relative / absolute here because of Mozilla -->
- @synthesize是新特征,简化setter/getter方法;
- 能用retain/copy/assing,strong/wak管理内存
-
在init和dealloc函数中,不建议用synthesized accessors,它会trigger KVO
notifications;它本质上是setter/getter,在不需要调用setter/getter的时候,就不要用
-
32bit Mac OS X不支持Dynamic ivar,必须手动什么iVar,否则无法编译
-
调试原因(我还不清楚这是什么意思,因为在self变量里可以找到ivar):For some reason the XCode
debugger doesn't show properties that don't have corresponding ivars
explicitly declared.
self.abc = nil 与 _abc = nil的本质区别
分享到:
相关推荐
积分已经设成最低了,可解析爱立信MRR、NCS 2进制文件。
ericsson的MRR/FAS/NCS指令,及注意事项。
MRR应用介绍,移动通信网络优化对于 MRR 统计数据的应用
爱立信MRR的定义,分析过程,入门简介,合适刚入门的网优人员
一个可以分析MRR数据的软件,通过mrr测试数据,进行数据分析。
论文研究-MRR模型一类正态总体的极大似然估计法及检验.pdf, Madhavan等给出了一个著名的价差分解模型:MRR模型. MRR模型利用广义矩方法估计参数,这使得该模型在扩展上...
MRR。非对准人脸识别。
NCS_MRR_FAS_CTR_MTR SC文件是否定义完全需分IOG及APG网元,IOG网元的定义一般情况下基本无误,APG网元则有个别会定义出现错误
mrr2c 将Metek MRR-2微型雨雷达数据文件转换为NetCDF。 mrr2c是一个开源程序,可将Metek Micro Rain Radar 2(MRR-2)数据转换为NetCDF。 支持RAW,PRO和AVE文件。 注意:早期版本生成HDF5文件。 如果需要这种类型的...
关于锥筒区域的细节:见CAD 三维数据,如果客户有需求,博世可提供该数据。在锥筒区 域内,水平及垂直方向+/-3°的安装偏差角已经考虑进去。而在x/y/z 三个方向的安装偏移量未被考虑,需要单独进行考虑
MRR 是 Multi-Range Read 的简写,目的是减少磁盘随机访问,将随机访问转化为较为顺序的访问。适用于 range/ref/eq_ref 类型的查询。 实现原理: 1、在二级索引查找后,根据得到的主键到聚簇索引找出需要的数据。 2...
NULL 博文链接:https://chuanwang66.iteye.com/blog/1138712
这是用于质谱研究的R软件包。 MrR可用于分析质谱数据,以进行基线校正,降噪,峰检测,峰比对,峰归一化和生物标记物发现。
通达信指标公式源码 极品MRR抄底副图指标.doc
雷达传感器控制单元(以下亦称雷达、中距离雷达、雷达传感器、传感器等)中集成... 雷达前方的目标反射雷达信号,相对速度和距离可以通过多普勒效应和差频算出。目标的方位角则通过使用规范化的天线特性图来计算得到。
利用认知无线电技术在长期演进铁路(LTE-R)通信系统中构建认知无线网络,提出认知 LTE-R 基站网络架构和MRR调度算法,用以提高铁路专网频谱利用率。特别针对单一车载网关中认知LTE-R基站的先入先出队列策略造成次...
电信设备-双工逆向调制MRR自由空间激光通信FSO系统.zip
传统全通型微环(All-pass microring)的matlab仿真代码,以及部分参数结果图