Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2600587
  • 博文数量: 877
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5920
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-05 12:25
个人简介

技术的乐趣在于分享,欢迎多多交流,多多沟通。

文章分类

全部博文(877)

文章存档

2021年(2)

2016年(20)

2015年(471)

2014年(358)

2013年(26)

分类: iOS平台

2015-09-18 07:33:19

IOS开发之内存管理--dealloc该写些什么
http://blog.csdn.net/xdrt81y/article/details/10107409

在非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问题了。系统将自动的管理内存。
阅读(635) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~