Iphone 在目标C中释放对象时
当我必须释放一个物体时,我在某些情况下感到困惑?所以我想知道我们什么时候在objective C中释放对象。我可以在分配对象的地方使用autorelease吗?autorelease有什么缺点吗?在何处释放以下对象 案例1:Iphone 在目标C中释放对象时,iphone,ios,memory-management,Iphone,Ios,Memory Management,当我必须释放一个物体时,我在某些情况下感到困惑?所以我想知道我们什么时候在objective C中释放对象。我可以在分配对象的地方使用autorelease吗?autorelease有什么缺点吗?在何处释放以下对象 案例1: SelectFrame *obj=[[SelectFrame alloc]initWithNibName:@"SelectFrame" bundle:nil]; [[self navigationController] pushViewController:obj anim
SelectFrame *obj=[[SelectFrame alloc]initWithNibName:@"SelectFrame" bundle:nil];
[[self navigationController] pushViewController:obj animated:YES];
案例2:
UIView *barView=[[UIView alloc]initWithFrame:CGRectMake(0, 500, 200,50)];
barView.backgroundColor=[UIColor redColor];
[self.view addSubview:barView];
UIView *barView=[[UIView alloc]initWithFrame:CGRectMake(0, 500, 200,50)];
barView.backgroundColor=[UIColor redColor];
[self.view addSubview:barView];
[barView release];
案例3:
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://xxxxxxxxxxxxxxxxx"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://xxxxxxxxxxxxxxxxx"]];
[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
是的,你必须为上述两种情况放行 案例1:
SelectFrame *obj=[[SelectFrame alloc]initWithNibName:@"SelectFrame" bundle:nil];
[[self navigationController] pushViewController:obj animated:YES];
[obj release];
UIView *barView=[[UIView alloc]initWithFrame:CGRectMake(0, 500, 200,50)];
barView.backgroundColor=[UIColor redColor];
[self.view addSubview:barView];
[barView release];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://xxxxxxxxxxxxxxxxx"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
案例2:
SelectFrame *obj=[[SelectFrame alloc]initWithNibName:@"SelectFrame" bundle:nil];
[[self navigationController] pushViewController:obj animated:YES];
[obj release];
UIView *barView=[[UIView alloc]initWithFrame:CGRectMake(0, 500, 200,50)];
barView.backgroundColor=[UIColor redColor];
[self.view addSubview:barView];
[barView release];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://xxxxxxxxxxxxxxxxx"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
案例3:
SelectFrame *obj=[[SelectFrame alloc]initWithNibName:@"SelectFrame" bundle:nil];
[[self navigationController] pushViewController:obj animated:YES];
[obj release];
UIView *barView=[[UIView alloc]initWithFrame:CGRectMake(0, 500, 200,50)];
barView.backgroundColor=[UIColor redColor];
[self.view addSubview:barView];
[barView release];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://xxxxxxxxxxxxxxxxx"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
这里不需要释放,因为请求对象处于自动释放模式
记住两件事。
1.)当您保留
或alloc init
该对象时,必须手动释放该对象
2.)没有alloc方法的类方法返回一个自动释放的
对象,因此不需要释放这些对象
使用自动释放的缺点
:
SelectFrame *obj=[[SelectFrame alloc]initWithNibName:@"SelectFrame" bundle:nil];
[[self navigationController] pushViewController:obj animated:YES];
[obj release];
UIView *barView=[[UIView alloc]initWithFrame:CGRectMake(0, 500, 200,50)];
barView.backgroundColor=[UIColor redColor];
[self.view addSubview:barView];
[barView release];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://xxxxxxxxxxxxxxxxx"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
好的,autorelease
是什么意思?自动释放意味着,不是我们,而是我们的应用程序将决定何时释放对象。假设你的问题是第二种情况。将barView
添加到self.view
后,不需要此分配对象。因此,我们发布它。但是,如果我们将它保持在autorelease
模式,应用程序将保留它更长的时间,仍然保留该对象会浪费部分内存。因此,我们不应该在这里使用自动释放
使用自动释放的优点
:
SelectFrame *obj=[[SelectFrame alloc]initWithNibName:@"SelectFrame" bundle:nil];
[[self navigationController] pushViewController:obj animated:YES];
[obj release];
UIView *barView=[[UIView alloc]initWithFrame:CGRectMake(0, 500, 200,50)];
barView.backgroundColor=[UIColor redColor];
[self.view addSubview:barView];
[barView release];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://xxxxxxxxxxxxxxxxx"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
这是一个非常流行的例子
- (NSString*) getText
{
NSString* myText = [[NSString alloc] initWithFormat:@"Hello"];
return myText;
}
这里,第3行导致泄漏,因为我们没有释放分配给myText
的内存。
因此,
解决方案
使用ARC,忘记
保留
释放
:)是的,对于上述两种情况,您必须释放
案例1:
SelectFrame *obj=[[SelectFrame alloc]initWithNibName:@"SelectFrame" bundle:nil];
[[self navigationController] pushViewController:obj animated:YES];
[obj release];
UIView *barView=[[UIView alloc]initWithFrame:CGRectMake(0, 500, 200,50)];
barView.backgroundColor=[UIColor redColor];
[self.view addSubview:barView];
[barView release];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://xxxxxxxxxxxxxxxxx"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
案例2:
SelectFrame *obj=[[SelectFrame alloc]initWithNibName:@"SelectFrame" bundle:nil];
[[self navigationController] pushViewController:obj animated:YES];
[obj release];
UIView *barView=[[UIView alloc]initWithFrame:CGRectMake(0, 500, 200,50)];
barView.backgroundColor=[UIColor redColor];
[self.view addSubview:barView];
[barView release];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://xxxxxxxxxxxxxxxxx"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
案例3:
SelectFrame *obj=[[SelectFrame alloc]initWithNibName:@"SelectFrame" bundle:nil];
[[self navigationController] pushViewController:obj animated:YES];
[obj release];
UIView *barView=[[UIView alloc]initWithFrame:CGRectMake(0, 500, 200,50)];
barView.backgroundColor=[UIColor redColor];
[self.view addSubview:barView];
[barView release];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://xxxxxxxxxxxxxxxxx"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
这里不需要释放,因为请求对象处于自动释放模式
记住两件事。
1.)当您保留
或alloc init
该对象时,必须手动释放该对象
2.)没有alloc方法的类方法返回一个自动释放的
对象,因此不需要释放这些对象
使用自动释放的缺点
:
SelectFrame *obj=[[SelectFrame alloc]initWithNibName:@"SelectFrame" bundle:nil];
[[self navigationController] pushViewController:obj animated:YES];
[obj release];
UIView *barView=[[UIView alloc]initWithFrame:CGRectMake(0, 500, 200,50)];
barView.backgroundColor=[UIColor redColor];
[self.view addSubview:barView];
[barView release];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://xxxxxxxxxxxxxxxxx"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
好的,autorelease
是什么意思?自动释放意味着,不是我们,而是我们的应用程序将决定何时释放对象。假设你的问题是第二种情况。将barView
添加到self.view
后,不需要此分配对象。因此,我们发布它。但是,如果我们将它保持在autorelease
模式,应用程序将保留它更长的时间,仍然保留该对象会浪费部分内存。因此,我们不应该在这里使用自动释放
使用自动释放的优点
:
SelectFrame *obj=[[SelectFrame alloc]initWithNibName:@"SelectFrame" bundle:nil];
[[self navigationController] pushViewController:obj animated:YES];
[obj release];
UIView *barView=[[UIView alloc]initWithFrame:CGRectMake(0, 500, 200,50)];
barView.backgroundColor=[UIColor redColor];
[self.view addSubview:barView];
[barView release];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://xxxxxxxxxxxxxxxxx"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
这是一个非常流行的例子
- (NSString*) getText
{
NSString* myText = [[NSString alloc] initWithFormat:@"Hello"];
return myText;
}
这里,第3行导致泄漏,因为我们没有释放分配给myText
的内存。
因此,
解决方案
使用ARC,忘记
retain
release
:)如果在3种情况下使用ARC,则无需释放任何东西,只要明智地使用即可(如果需要,请分配)
如果不使用ARC,则需要释放
现在案例1:
SelectFrame *obj=[[SelectFrame alloc]initWithNibName:@"SelectFrame" bundle:nil];
[[self navigationController] pushViewController:obj animated:YES];
[obj release];
案例2:
UIView *barView=[[UIView alloc]initWithFrame:CGRectMake(0, 500, 200,50)];
barView.backgroundColor=[UIColor redColor];
[self.view addSubview:barView];
UIView *barView=[[UIView alloc]initWithFrame:CGRectMake(0, 500, 200,50)];
barView.backgroundColor=[UIColor redColor];
[self.view addSubview:barView];
[barView release];
案例3:
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://xxxxxxxxxxxxxxxxx"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://xxxxxxxxxxxxxxxxx"]];
[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
参考链接。如果在3种情况下使用ARC,则无需发布任何内容,只要明智地使用即可(如果需要,请分配) 如果不使用ARC,则需要释放 现在案例1:
SelectFrame *obj=[[SelectFrame alloc]initWithNibName:@"SelectFrame" bundle:nil];
[[self navigationController] pushViewController:obj animated:YES];
[obj release];
案例2:
UIView *barView=[[UIView alloc]initWithFrame:CGRectMake(0, 500, 200,50)];
barView.backgroundColor=[UIColor redColor];
[self.view addSubview:barView];
UIView *barView=[[UIView alloc]initWithFrame:CGRectMake(0, 500, 200,50)];
barView.backgroundColor=[UIColor redColor];
[self.view addSubview:barView];
[barView release];
案例3:
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://xxxxxxxxxxxxxxxxx"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://xxxxxxxxxxxxxxxxx"]];
[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
参考链接。感谢您的回答,我还有一个问题,我使用的是JSON,JSON sdk.m文件中很少有对象显示内存泄漏?当我启用ARC时,这些文件会显示错误。如何将JSON sdk与ARCI结合使用我不太了解您正在使用的代码。但是你可以用谷歌搜索类似的东西。感谢您的回答,我还有一个问题,我正在使用JSON,JSON sdk.m文件中很少有对象显示内存泄漏?当我启用ARC时,这些文件会显示错误。如何将JSON sdk与ARCI结合使用我不太了解您正在使用的代码。但是你可以用谷歌搜索类似的东西。