iOS中自动释放问题?
--前言:iOS开发中关于对象的释放问题,虽然知道规则,但总不清楚自动释放的对象什么时候彻底消失?它存在的多久?什么情况会消失?都不清楚,每次用自动释放对象,总有点心虚的感觉,以下是一些例子、研究。
--直接上代码,代码写在一个控制器的viewDidLoad方法里(代码内容是用一个path就保存一个data,但path是自动释放对象):
[html]
例子1
NSArray*patharray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString*docstr=[patharray objectAtIndex:0];
//path成员变量,文件路径-自动释放对象
path=[docstr stringByAppendingPathComponent:@"1.png"];
//data读取内容
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"IMG_1340" ofType:@"PNG"];
NSData *data= [NSData dataWithContentsOfFile:imagePath];
//用path去保存data
[data writeToFile:path atomically:NO];
//例子1
NSArray*patharray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString*docstr=[patharray objectAtIndex:0];
//path成员变量,文件路径-自动释放对象
path=[docstr stringByAppendingPathComponent:@"1.png"];
//data读取内容
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"IMG_1340" ofType:@"PNG"];
NSData *data= [NSData dataWithContentsOfFile:imagePath];
//用path去保存data
[data writeToFile:path atomically:NO];例子1,这么写,没问题,文件能保存。 [html] view plaincopyprint? //例子2
NSArray*patharray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString*docstr=[patharray objectAtIndex:0];
//path成员变量,文件路径-自动释放对象
path=[docstr stringByAppendingPathComponent:@"1.png"];
//调用方法,path做参数
[self saveData:path];
//分割线
-(void)saveData:(NSString *)filePath
{
//data读取内容
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"IMG_1340" ofType:@"PNG"];
NSData *data= [NSData dataWithContentsOfFile:imagePath];
//用path去保存data
[data writeToFile:filePath atomically:NO];
}
//例子2
NSArray*patharray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString*docstr=[patharray objectAtIndex:0];
//path成员变量,文件路径-自动释放对象
path=[docstr stringByAppendingPathComponent:@"1.png"];
//调用方法,path做参数
[self saveData:path];
//分割线
-(void)saveData:(NSString *)filePath
{
//data读取内容
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"IMG_1340" ofType:@"PNG"];
NSData *data= [NSData dataWithContentsOfFile:imagePath];
//用path去保存data
[data writeToFile:filePath atomically:NO];
} 例子2,这么写,也没问题,文件能保存。或者[self saveData:path]; 改成[self saveData]; saveData方法不传入参数,用成员变量path代替filePath的作用,这么写,也没问题。[html] view plaincopyprint? //例子3
NSArray*patharray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString*docstr=[patharray objectAtIndex:0];
//path成员变量,文件路径-自动释放对象
path=[docstr stringByAppendingPathComponent:@"1.png"];
//创建按钮
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0.0, 0.0, 100, 100)];
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(saveData) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
-(void)saveData
{
//data读取内容
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"IMG_1340" ofType:@"PNG"];
NSData *data= [NSData dataWithContentsOfFile:imagePath];
//用path去保存data
[data writeToFile:path atomically:NO];
}
//例子3
NSArray*patharray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString*docstr=[patharray objectAtIndex:0];
//path成员变量,文件路径-自动释放对象
path=[docstr stringByAppendingPathComponent:@"1.png"];
//创建按钮
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0.0, 0.0, 100, 100)];
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(saveData) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
-(void)saveData
{
//data读取内容
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"IMG_1340" ofType:@"PNG"];
NSData *data= [NSData dataWithContentsOfFile:imagePath];
//用path去保存data
[data writeToFile:path atomically:NO];
}例子3,点击button,程序会崩溃,提示“Thread 1:EXC_BAD_ACCESS(code =1, address = 0x0000008)” 。但如果将 path=[docstr stringByAppendingPathComponent:@"1.png"];改成path = [NSString stringWithString:[docstr stringByAppendingPathComponent:@"1.png"]];程序也行,不会崩溃。
--综上看来,“系统返回的自动释放对象”它的存在周期,在一个方法里是有效的(例子1/2),但离开了这个方法,在别的方法,别的地方就不能使用了(例子3),同时“自己创建的自动释放对”比“系统返回的自动释放对象”存在周期长点(例子3改动),但也不是总是存在,有时候用着用着就没了(曾经碰到过,一个自己创建的自动释放的数组,程序运行久了,有时崩溃,就是用着用着该数组没了)。
--总结,如果一个对象你要用,之前retain,用完了就release,这样能确保自动释放对象消息。(例子3中将 path=[docstr stringByAppendingPathComponent:@"1.png"]; 改为path = [NSString stringWithString:[docstr stringByAppendingPathComponent:@"1.png"]] 之后,程序正常运行)
补充:移动开发 , IOS ,