Alloc/Release question
Hi guys,
I have a very basic question, I'm a java programmer and I'm not really familiar with alloc/release/..
I've an NSMutableArray called nextBalls containing UIImageViews:
My question: Do I have to release the allocated UIImageViews in my NSMutableArray nextBalls if not needed anymore? Or are they automatically released if I apply
?
Someone said I have to release all objects that i allocated, that's why im asking!
Thanks in advance
I have a very basic question, I'm a java programmer and I'm not really familiar with alloc/release/..
I've an NSMutableArray called nextBalls containing UIImageViews:
Code:
[nextBalls addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:[self randomImageName]]]];My question: Do I have to release the allocated UIImageViews in my NSMutableArray nextBalls if not needed anymore? Or are they automatically released if I apply
Code:
[nextBalls removeObjectAtIndex:0];Someone said I have to release all objects that i allocated, that's why im asking!
Thanks in advance
The array retains it on adding, releases it on removal. So think of it this way:
1) You create the image: retain=1
2) You add to the array: retain=2
3) You remove from the array: retain=1
4) You release it again: retain=0, it is deleted
This is where autorelease comes into play. Autorelease the image after creating it, and its retain will be decremented by one in the next event loop. This way you can basically forget about it, and when it's removed from the array, so long as nobody else has retained it it will be freed.
Garbage collection is obviously preferable, but as manual memory management goes, retain/release/autorelease aren't too bad.
1) You create the image: retain=1
2) You add to the array: retain=2
3) You remove from the array: retain=1
4) You release it again: retain=0, it is deleted
This is where autorelease comes into play. Autorelease the image after creating it, and its retain will be decremented by one in the next event loop. This way you can basically forget about it, and when it's removed from the array, so long as nobody else has retained it it will be freed.
Code:
UIImageView *view = [[UIImageView alloc] initWithImage: [UIImage imageNamed:[self randomImage]]];
[nextBalls addObject: [view autorelease]];
Ok I do understand it now! Thank you for that excellent explanation!
Note that most of Cocoa's default factory methods (those that obscure alloc, such as +[NSMutableArray array]) automatically apply autorelease, thus requiring a retain call.
So if in some object initialization method, you must either say myArray = [[NSMutableArray array] retain]; or myArray = [[NSMutableArray alloc] init];
So if in some object initialization method, you must either say myArray = [[NSMutableArray array] retain]; or myArray = [[NSMutableArray alloc] init];
"Must" is not so. "Likely want to" is more appropriate. It depends on how the array is being used.
yes, of course =)
If a variable is intended for local use, retaining will result in memory leaks. Thanks for catching my mistake
If a variable is intended for local use, retaining will result in memory leaks. Thanks for catching my mistake
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| yet another retain/release question... | Andrew | 3 | 2,602 |
May 30, 2005 03:21 PM Last Post: Andrew |
|

