您的位置:首页 > 其它

简述OC中内存管理机制。

2015-02-11 10:53 176 查看
1 简述OC中内存管理机制。与retain配对使用的方法是dealloc还是release,为什么?需要与alloc配对使用的方法是dealloc还是release,为什么?readwrite,readonly,assign,retain,copy,nonatomic 、atomic、strong、weak属性的作用?

OC使用了一种叫做引用计数的机制来管理对象,如果对一个对象使用了alloc、[Mutable]copy,retain,那么你必须使用相应的realease或者autorelease。也可以理解为自己生成的对象,自己持有。非自己生成的对象,自己也能持有。不在需要自己持有的对象时释放。非自己持有的对象无法释放。生成并持有对象<alloc,new,copy,mutableCopy等>,持有对象<retain>,释放对象<release>,废弃对象<dealloc>。readwrite(默认):可读可写,表示既有getter方法,也有setter方法。readonly:表示只有getter方法,没有setter方法。nonatomic:不考虑线程安全。atomic(默认):线程操作安全。strong(默认):ARC下和MRC下retain一样,weak(ARC下):和(MRC下)assign类似,区别是当weak指向的内存释放掉后自动置为nil,防止野指针。
unsafe_unretained声明一个若引用,但不会自动置为nil,可能会出现野指针。
线程安全下的setter和getter方法:
- (NSString *)value{
@synchronized(self){
return [[_value retain] autorelease];
}
}
- (void)setValue:(NSString *)aValue{
@synchronized(self){
[aValue retain];
[_value release];
_value = aValue;
}
}

对于dealloc函数有两种做法,

一个是直接将实例变量release掉:

-(void)dealloc



[subject release];

[super dealloc];



另一种是将变量relsease 掉再将它指向nil;

-(void)dealloc



[subject release]

subject=nil;

[super dealloc];



两种方法结果是一致的,但是有些许的差别。

变量在被release掉后,系统将该内存标识为可用,nil只是起到重置指针的作用。

但是在object-c中给nil对象发送消息是,什么也不会发生,这样在调试的时候,很难找到出错的地方,所以

在调试阶段最后用第一种,为了上线的时候用第二种,

可以通过宏定义

#if DEBUG

#define MCRelease(x) [x release]

#else

#define MCRelease(x) [x release], x = nil

#endif

在非ARC开发环境中,dealloc是类释放前,清理内存的最后机会。到底那些变量和属性该释放呢,一些特殊的类(nstimer,observer)该怎么释放。需要注意的是不释放会引起内存泄露,过度释放也会引起内存泄露,接下来会慢慢展开:

1 变量的释放

变量声明

@interface EnterHondaViewController : UIViewController{

UIImageView * imageLogo;

UIButton * btn_Corporate;

UIButton * btn_Brand;

CorporateView * corporateview;

BrandView * brandview;

}

变量初始化

@implementation EnterHondaViewController

-(id)initWithFrame:(CGRect)frame

{

self = [super init];

if (self) {

self.view.frame = frame;

[self.view setBackgroundColor:[UIColor whiteColor]];

UIImageView * background = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,1024, 768)];

[self.view addSubview:background];

[background release];

[background setImage:[UIImage imageNamed:@"AllAuto_Image_BG"]];

UIButton * backBtn = [[UIButton alloc]initWithFrame:CGRectMake(50, 18, 55,40)];

[backBtn setImage:[UIImage imageNamed:@"home_button"] forState:UIControlStateNormal];

[backBtn addTarget:self action:@selector(onBack:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:backBtn];

[backBtn release];

UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 25, 400, 35)];

titleLabel.backgroundColor = [UIColor clearColor];

titleLabel.textAlignment = NSTextAlignmentLeft;

titleLabel.font = [UIFont fontWithName:@"Arial" size:30];

titleLabel.textColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1.0];

[self.view addSubview:titleLabel];

[titleLabel release];

[titleLabel setText:@"走进广本"];

UIImageView * lineView = [[UIImageView alloc] initWithFrame:CGRectMake((frame.size.width-658)/2,70, 658, 8)];

lineView.image = [UIImage imageNamed:@"AllAuto_Config_TimeLine_BG"];

[self.view addSubview:lineView];

[lineView release];

UIView * logoview = [[UIView alloc] initWithFrame:CGRectMake(780, 80, 240, 25)];

[self.view addSubview:logoview];

[logoview release];

imageLogo = [[UIImageView alloc] initWithFrame:CGRectMake(0, 6, 12, 13)];

[logoview addSubview:imageLogo];

[imageLogo release];

imageLogo.image = [UIImage imageNamed:@"AllAuto_Corporation_Button_Select"];

btn_Corporate = [[UIButton alloc] initWithFrame:CGRectMake(13, 0, 100, 25)];

[logoview addSubview:btn_Corporate];

btn_Corporate.tag = 1;

[btn_Corporate release];

[btn_Corporate addTarget:self action:@selector(onOptionClick:) forControlEvents:UIControlEventTouchUpInside];

[btn_Corporate setTitle:@"企业文化" forState:UIControlStateNormal];

btn_Brand = [[UIButton alloc] initWithFrame:CGRectMake(133, 0, 100, 25)];

[logoview addSubview:btn_Brand];

btn_Brand.tag = 3;

[btn_Brand release];

[btn_Brand addTarget:self action:@selector(onOptionClick:) forControlEvents:UIControlEventTouchUpInside];

[btn_Brand setTitle:@"品牌故事" forState:UIControlStateNormal];

corporateview = [[CorporateView alloc] initWithFrame:CGRectMake(0, 110, frame.size.width, frame.size.height-120)];

brandview = [[BrandView alloc] initWithFrame:CGRectMake(0, 110, frame.size.width, frame.size.height-110)];

[btn_Corporate sendActionsForControlEvents:UIControlEventTouchUpInside];

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onGoBrandPage:) name:Notification_Navigate_To_Brand object:nil];

}

return self;

}

变量释放

-(void)dealloc

{

[[NSNotificationCenter defaultCenter]removeObserver:self];

[brandview release];

[corporateview release];

[super dealloc];

}

2 属性的释放

属性声明

@interface GGDetailCell : UIGridViewCell {

}

@property (nonatomic, retain) UILabel *labelTitle;

@end

属性初始化

- (id)init {

if (self = [super init]) {

self.frame = CGRectMake(0, 0, 66, 30.5);

{ // labelTitle

self.labelTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 7, 66, 16)] autorelease];

[self.labelTitle setBackgroundColor:[UIColor clearColor]];

[self.labelTitle setTextColor:TEXT_COLOR_PART_NORMAL];

[self.labelTitle setFont:[UIFont systemFontOfSize:12]];

[self.labelTitle setNumberOfLines:0];

[self.labelTitle setTextAlignment:UITextAlignmentCenter];

[self addSubview:self.labelTitle];

}

}

return self;

}

属性释放

- (void)dealloc {

self.labelTitle = nil;

[super dealloc];

}

3 定时器的释放

定时器声明:

@interface BRLandscapeView

NSTimer* timer;

}

@end

定期初始化:

-(void) myInit{

{//set timer

timer = [NSTimer scheduledTimerWithTimeInterval: 1

target: self

selector: @selector(handleTimer:)

userInfo: nil

repeats: YES];

}

定时器释放:如果实在view中声明初始化的,要在 controller中view释放前先释放定时器,否则由于循环引用,而释放不掉

@implementation BookReadController

-(void)dealloc

{

if (landscape) {

[landscape->timer invalidate];

}

SafeRelease(landscape);

[super dealloc];

}

@end

4 通知的释放

-(void)dealloc

{

[[NSNotificationCenter defaultCenter]removeObserver:self];

[super dealloc];

}

5 delegate的释放

delegate属性的赋值一般为self,虽然声明时assign,但在相关的view释放时,在之前先释放掉delegate

情况一

if (_loadingContentView) {

_loadingContentView.delegate = nil;

[_loadingContentView removeFromSuperview];

}

情况二

self.partGridView.uiGridViewDelegate = nil;

self.partGridView = nil;

6 有返回值的函数的内存管理

如果一个函数需要一个返回值,此返回值在函数内声明和初始化,但缺不能立即释放,最好的处理方式如下:

-(NSArray*)getTemplatesByPath:(NSString *)path

{

NSError * error = nil;

NSArray* files = [fileManager contentsOfDirectoryAtPath:path error:&error];

if(error != nil)

return nil;

NSMutableArray* resultArray = [NSMutableArray new];

for (NSInteger index=0; index < [files count]; index++)

{

NSString* fileName = [files objectAtIndex:index];

NSString* extType = [fileName pathExtension];

if(NO == [extType isEqualToString:@"tpl"])

continue;

[resultArray addObject:fileName];

}

return [resultArray autorelease];

}

扩展

1 变量初始化完,用完可立即释放的情况

UIImageView * lineView = [[UIImageView alloc] initWithFrame:CGRectMake((frame.size.width-658)/2,70, 658, 8)];

lineView.image = [UIImage imageNamed:@"AllAuto_Config_TimeLine_BG"];

[self.view addSubview:lineView];

[lineView release];

2 声明时,是声明为变量还是属性的考量。

在ios第一版中,我们为输出口同时声明了属性和底层实例变量,那时,属性是oc语言的一个新的机制,并且要求你必须声明与之对应的实例变量,例如:

@interface MyViewController :UIViewController

{

UIButton *myButton;

}

@property (nonatomic, retain) UIButton *myButton;

@end

最近,苹果将默认编译器从GCC转换为LLVM(low level virtual machine),从此不再需要为属性声明实例变量了。

如果LLVM发现一个没有匹配实例变量的属性,它将自动创建一个以下划线开头的实例变量。因此,在这个版本中,我们不再为输出口声明实例变量。

例如:

MyViewController.h文件

@interface MyViewController :UIViewController

@property (nonatomic, retain) UIButton *myButton;

@end

在MyViewController.m文件中

编译器也会自动的生成一个实例变量_myButton

那么在.m文件中可以直接的使用_myButton实例变量,也可以通过属性self.myButton.都是一样的。

注意这里的self.myButton其实是调用的myButton属性的getter/setter方法

这与c++中点的使用是有区别的,c++中的点可以直接访问成员变量(也就是实例变量)

例如在oc中有如下代码

.h文件

@interface MyViewController :UIViewController

{

NSString *name;

}

@end

.m文件中

self.name 这样的表达式是错误的。xcode会提示你使用->,改成self->name就可以了。

因为oc中点表达式是表示调用方法,而上面的代码中没有name这个方法。

oc语法关于点表达式的说明:

"点表达式(.)看起来与C语言中的结构体访问以及java语言汇总的对象访问有点类似,其实这是oc的设计人员有意为之。

如果点表达式出现在等号 = 左边,该属性名称的setter方法将被调用。如果点表达式出现在右边,该属性名称的getter方法将被调用。"

所以在oc中点表达式其实就是调用对象的setter和getter方法的一种快捷方式, 例如:

dealie.blah = greeble 完全等价于 [dealie.blah setBlah:greeble];

以前的用法,声明属性跟与之对应的实例变量:

@interface MyViewController :UIViewController

{

UIButton *myButton;

}

@property (nonatomic, retain) UIButton *myButton;

@end

这种方法基本上使用最多,现在大部分也是在使用,因为很多开源的代码都是这种方式。

但是ios5更新之后,苹果是建议以以下的方式来使用

@interface MyViewController :UIViewController

@property (nonatomic, retain) UIButton *myButton;

@end

因为编译器会自动为你生成以下划线开头的实例变量_myButton。不需要自己手动再去写实例变量。

而且也不需要在.m文件中写@synthesize myButton;也会自动为你生成setter,getter方法。

@synthesize的作用就是让编译器为你自动生成setter与getter方法。

它还有一个作用,可以指定与属性对应的实例变量,

例如@synthesize myButton = xxx;

那么self.myButton其实是操作的实例变量xxx,而不是_myButton了。

在实际的项目中,我们一般这么写.m文件

@synthesize myButton;

这样写了之后,那么编译器会自动生成myButton的实例变量,以及相应的getter和setter方法。

注意:_myButton这个实例变量是不存在的,因为自动生成的实例变量为myButton而不是_myButton。

所以现在@synthesize的作用就相当于指定实例变量,

如果.m文件中写了@synthesize myButton;那么生成的实例变量就是myButton。

如果没写@synthesize myButton;那么生成的实例变量就是_myButton。

所以跟以前的用法还是有点细微的区别。

注意:这里与类别中添加的属性要区分开来,因为类别中只能添加方法,不能添加实例变量。

经常会在ios的代码中看到在类别中添加属性,这种情况下,是不会自动生成实例变量的。

比如在

UINavigationController.h文件中会对UIViewController类进行扩展

@interface UIViewController (UINavigationControllerItem)

@property(nonatomic,readonly,retain) UINavigationItem *navigationItem;

@property(nonatomic) BOOL hidesBottomBarWhenPushed;

@property(nonatomic,readonly,retain) UINavigationController *navigationController;

@end

这里添加的属性,不会自动生成实例变量,这里添加的属性其实是添加的getter与setter方法。

注意一点,匿名类别(匿名扩展)是可以添加实例变量的,非匿名类别是不能添加实例变量的,只能添加方法,或者属性(其实也是方法)。

ARC

关于强引用,弱引用,arc的使用可以查看文件ios5arc完全指南。

ios5引进的arc确实方便了很多,ARC 的规则非常简单:

只要还有一个变量指向对象,对象就会保持在内存中。当指针指向新值,或者指针不再存在时,相关联的对象就会自动释放。

这条规则对于实例变量、synthesize 属性、本地变量都是适用的。

以前没有arc的时候,必须调用

dealloc方法来释放内存,比如:

- (void)dealloc {

[_myTableView release];

[superdealloc];

}

如果使用了ARC,那么将不再需要dealloc方法了,也不需要再担心release问题了。系统将自动的管理内存。

3 众所周知,iOS开发的时候,使用ARC的话,dealloc函数是不需要实现的,写了反而会出错。

但有些特殊的情况,dealloc函数还是需要的。

比如,在画面关闭的时候,需要把ViewController的某些资源释放,

在viewDidDissppear不一定合适,viewDidUnload一般情况下只在memory warning的时候才被调用。

不用ARC的情况下,我们自然会想到dealloc函数。

其实ARC环境下,也没有把dealloc函数禁掉,还是可以使用的。只不过不需要调用[supper dealloc]了。

举个例子,画面上有UIWebView,它的delegate是该画面的ViewController,在WebView载入完成后,需要做某些事情,比如,把indicator停掉之类的。

如果在WebView载入完成之前关闭画面的话,画面关闭后,ViewController也释放了。但由于WebView正在载入页面,而不会马上被释放,等到页面载入完毕后,回调delegate(ViewController)中的方法,由于此时ViewController已经被释放,所以会出错。(message sent to deallocated instance)

解决办法是在dealloc中把WebView的delegate释放。

-(void)dealloc {

self.webView.delegate = nil;

}

4 今天我给大家分享平时不太能注意到的一个内存释放问题!很多人不是不知道,而是没有注意到这块!因为这里是大家很不容易发现的问题,今天有个好友在群里突然问这个问题,我遇到过好多新手问同样的问题!今天我给大家分享一下自己的心得.

Objective-c 语言中最头疼的事就是内存释放,申明一个变量后记得一定要释放这个变量,在我的博客中已经有一些这方面的文章,我们定义的全局变量都是在 - (void)dealloc 函数中释放的;

里面继承了一个[super dealloc]方法,

有些同学平时自己释放内存都是写在 [super dealloc]的后面,但是在Objective-c 中不能这样写,所有的释放都必须写在 [super dealloc]的前面。

-------错误的写法--------

- (void)dealloc

{

[super dealloc];

[XXX release];

}

-------正确的写法--------

- (void)dealloc

{

[XXX release];

[super dealloc];

}

原因是:“你所创建的每个类都是从父类,根类继承来的,有很多实例变量也会继承过来,这部分变量有时候会在你的程序内使用,它们不会自动释放内存,你需要调用父类的 dealloc方法来释放,然而在此之前你需要先把自己所写类中的变量内存先释放掉,否则就会造成你本类中的内存积压,造成泄漏”.不过在IOS6有了ARC后就不用手动去释放了,也没有此函数了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: