Hi I generally create objects of another classes. can you please tel me if this wil be in the auto release pool? or should we release it manually.
Asked
Active
Viewed 118 times
-1
-
2You need to show some code or explain your question better, or both. Some objects you create might be autoreleased, others might not... – Carl Norum Jan 25 '11 at 06:55
2 Answers
1
if you init copy or new them you'll have to deallocate them if you put an autorlease with the allocation then they will be autoreleased
for example
Foo *foo = [[Foo alloc] init]; //you'll have release it somewhere yourself
And
Foo *foo = [[[Foo alloc] init] autorelease];// this will be autreleased

Asad Khan
- 11,469
- 13
- 44
- 59
1
The simple case is : if you use init, you are responsible for releasing it, either by calling release or by calling autorelease.
e.g.
NSString *myString = [NSString alloc] init]; // You need to release this
...
[myString release]; // Now it's released - don't use it again!
or if you are going give it to someone else
NSString *myString = [NSString alloc] init]; // This needs releasing
...
return [myString autorelease]; // You are finished with it but someone else might want it
However, there's a few other cases.
NSString *myString = [NSString stringWithFormat:@"hi"];
This object is in the autorelease pool already - don't release it!
NSString *secondString = [myString copy];
This object needs releasing - it is not autoreleased.
Rule of thumb : Anything with init, copy or new in the name - you made it, you release it. Anything else will be autoreleased.

deanWombourne
- 38,189
- 13
- 98
- 110
-
1The Rule of thumb is not quite correct: it's not the `init` but the `alloc` that demands a release, additionally it's missing `new`. – danyowdee Jan 28 '11 at 16:25
-