Possible Duplicate:
Cocoa Memory Management NSArray with Objects
I have an NSMutableArray filled with some objects.
For example:
...
id test = [NSObject new];
NSMutableArray *myArray = [NSMutableArray new];
[myArray addObject: test];
...
When more than one object need a reference to that array, I can’t just use the method “– removeAllObjects”.
So I only use the release of the array in my “- dealloc” method of my own classes.
- (void)dealloc {
[myArray release];
[super dealloc];
}
So is my object with the name “test” leaked and therefore I do need to do more?
I couldn’t find the answer in the documentation, the is no point for the “dealloc” or “release” method for NSMutableArray. And in the NSObject reference they don’t describe the NSMutableArray.
Could be it is anywhere in the “Memory Management Programming Guide” (hope so).
Only release what you own, whether through alloc, new, retain, or copy.
You own test after you do
so you are responsible for releasing it, whether you add it to an array or not.
If you do
you’ve already given up ownership of test, so you shouldn’t be releasing it, even if you put it in an array or some other form of collection. The collection will take ownership.
You only have to release the things you own.