Reputation: 1336
I am confused in some condition when I have to release an object? So I wants to know when we release objects in objective C. Can I use autorelease where I alloc the objects any disadvantage of autorelease? Where release the following objects?
Case 1:
SelectFrame *obj=[[SelectFrame alloc]initWithNibName:@"SelectFrame" bundle:nil];
[[self navigationController] pushViewController:obj animated:YES];
Case 2:
UIView *barView=[[UIView alloc]initWithFrame:CGRectMake(0, 500, 200,50)];
barView.backgroundColor=[UIColor redColor];
[self.view addSubview:barView];
Case 3:
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://xxxxxxxxxxxxxxxxx"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
Upvotes: 0
Views: 115
Reputation: 4746
Yes, you have to release for the Above two cases.
Case 1:
SelectFrame *obj=[[SelectFrame alloc]initWithNibName:@"SelectFrame" bundle:nil];
[[self navigationController] pushViewController:obj animated:YES];
[obj release];
Case 2:
UIView *barView=[[UIView alloc]initWithFrame:CGRectMake(0, 500, 200,50)];
barView.backgroundColor=[UIColor redColor];
[self.view addSubview:barView];
[barView release];
Case 3:
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://xxxxxxxxxxxxxxxxx"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
You don't need a release here, as the request object is in autoreleased mode.
Remember two things.
1.) You have to manually release an object when you retain
or alloc-init
that object.
2.) Class methods which do not have alloc methods return an autoreleased
object hence you don't need to release those objects.
Disadvantage of using autorelease
:
Ok, so what does autorelease
mean? Autorelease means , not us, but our App would decide when to release the object. Supposing case 2 of your question. After barView
is added to self.view
there is no need of this allocated object. Hence, we release it. But, if we had kept it in autorelease
mode, the App would keep it for longer time, wasting some part of memory by still keeping that object. Hence, we shouldn't use autorelease here.
Advantage of using autorelease
:
This over-popular example.
- (NSString*) getText
{
NSString* myText = [[NSString alloc] initWithFormat:@"Hello"];
return myText;
}
Here, the 3rd line causes a leak because we do not release the memory allocated to myText
.
Hence,
- (NSString*) getText
{
NSString* myText = [[[NSString alloc] initWithFormat:@"Hello"] autorelease];
return myText;
}
SOLUTION
Use ARC, forget retain
release
:)
Upvotes: 2
Reputation: 38259
If using ARC in 3 cases then no need to release anything just use wisely (alloc if needed)
If not using ARC then its needed to release
Now case 1 :
SelectFrame *obj=[[SelectFrame alloc]initWithNibName:@"SelectFrame" bundle:nil];
[[self navigationController] pushViewController:obj animated:YES];
[obj release];
Case 2:
UIView *barView=[[UIView alloc]initWithFrame:CGRectMake(0, 500, 200,50)];
barView.backgroundColor=[UIColor redColor];
[self.view addSubview:barView];
[barView release];
Case 3:
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://xxxxxxxxxxxxxxxxx"]];
[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
Refere How-to-avoid-memory-leaks-in-iPhone-applications link.
Upvotes: 1