在非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问题了。系统将自动的管理内存。

IOS开发之内存管理--dealloc该写些什么的更多相关文章

  1. IOS开发的内存管理

    关于IOS开发的内存管理的文章已经很多了,因此系统的知识点就不写了,这里我写点平时工作遇到的疑问以及解答做个总结吧,相信也会有人遇到相同的疑问呢,欢迎学习IOS的朋友请加ios技术交流群:190956 ...

  2. IOS开发小记-内存管理

    关于IOS开发的内存管理的文章已经很多了,因此系统的知识点就不写了,这里我写点平时工作遇到的疑问以及解答做个总结吧,相信也会有人遇到相同的疑问呢,欢迎学习IOS的朋友请加ios技术交流群:190956 ...

  3. [转载]对iOS开发中内存管理的一点总结与理解

    对iOS开发中内存管理的一点总结与理解   做iOS开发也已经有两年的时间,觉得有必要沉下心去整理一些东西了,特别是一些基础的东西,虽然现在有ARC这种东西,但是我一直也没有去用过,个人觉得对内存操作 ...

  4. iOS开发ARC内存管理技术要点

    本文来源于我个人的ARC学习笔记,旨在通过简明扼要的方式总结出iOS开发中ARC(Automatic Reference Counting,自动引用计数)内存管理技术的要点,所以不会涉及全部细节.这篇 ...

  5. (转)iOS开发ARC内存管理技术要点

    转自:http://www.cnblogs.com/flyFreeZn/p/4264220.html 本文来源于我个人的ARC学习笔记,旨在通过简明扼要的方式总结出iOS开发中ARC(Automati ...

  6. iOS开发ARC内存管理

    本文的主要内容: ARC的本质 ARC的开启与关闭 ARC的修饰符 ARC与Block ARC与Toll-Free Bridging ARC的本质 ARC是编译器(时)特性,而不是运行时特性,更不是垃 ...

  7. iOS开发--漫谈内存管理(一)

    1.MRC与ARC 苹果提供两种内存管理机制:一种是MRC(manual reference count),即手动引用计数:还有一种是ARC(auto reference count).即自己主动引用 ...

  8. iOS夯实:内存管理

    iOS夯实:内存管理 文章转自 内存管理 最近的学习计划是将iOS的机制原理好好重新打磨学习一下,总结和加入自己的思考. 有不正确的地方,多多指正. 目录: 基本信息 旧时代的细节 新时代 基本信息 ...

  9. 高性能JAVA开发之内存管理

    这几天在找一个程序的bug,主要是java虚拟机内存溢出的问题,调研了一些java内存管理的资料,现整理如下: 一.JVM中的对象生命周期 对象的生命周期一般分为7个阶段:创建阶段,应用阶段,不可视阶 ...

随机推荐

  1. Wap站总结一

    前段时间负责了公司的wap站前端工作,目前wap站的基础及较为复杂的几张页面都已经出来,现根据自己的一些经验,贴出部分心得,希望对现在或者以后可能会接触到Wap站的一些人有些帮助 一.本次WAP网站的 ...

  2. c#面向对象小结

    特点: 1:将复杂的事情简单化. 2:面向对象将以前的过程中的执行者,变成了指挥者. 3:面向对象这种思想是符合现在人们思考习惯的一种思想. 过程和对象在我们的程序中是如何体现的呢?过程其实就是函数: ...

  3. ruby环境sass编译中文出现Syntax error: Invalid GBK character错误解决方法

    sass文件编译时候使用ruby环境,无论是界面化的koala工具还是命令行模式的都无法通过,真是令人烦恼. 容易出现中文注释时候无法编译通过,或者出现乱码,找了几天的解决方法终于解决了. 这个问题的 ...

  4. php类的方法

    方法就是在类中的function,很多时候我们分不清方法与函数有什么差别,在面向过程的程序设计中function叫做函数,在面向对象中function则被称之为方法. 同属性一样,类的方法也具有pub ...

  5. Custom template tags and filters

    Code layout Custom template tags and filters must live inside a Django app. If they relate to an exi ...

  6. HDU1004 (数组元素出现最多)

    HDU1004 思路:求数组中出现次数最多的那个元素: 遍历数组元素,找出每个元素出现的次数 Input Input contains multiple test cases. Each test c ...

  7. LinkedHashMap和HashMap的比较使用(转载)

    LinkedHashMap和HashMap的比较使用 ? import java.util.HashMap; import java.util.Iterator; import java.util.L ...

  8. tabbar - 取消系统渲染

    /**  1. 取消自动渲染 - 图片***/ viewCon1.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_home_ ...

  9. APNs推送, 处理通知

    设备接到apns发来的通知,应用处理通知有以下几种情况: 1. 应用还没有加载 这时如果点击通知的显示按钮,会调用didFinishLaunchingWithOptions,不会调用didReceiv ...

  10. django中form表单的提交:

    一,关于表单: 表单在百度百科的解释:   表单在网页中主要负责数据采集功能.一个表单有三个基本组成部分: 表单标签:这里面包含了处理表单数据所用CGI程序的URL以及数据提交到服务器的方法. 表单域 ...