Preventing to alloc the same array more than once
A beginner's problem: I have a method which puts data into a MutableArray. Potentially, this method can be called more than once and I am a bit concerned that it will leak memory as I am allocating the array every time it gets called:
indexContent = [[NSMutableArray alloc] init];
int numberOfEntries = [noteBookContent count]/3;
for (int k=0; k < numberOfEntries; k++) {
IndexItem *newItem = [[IndexItem alloc] init];
newItem.itemTitle = [noteBookContent objectAtIndex:(k*3)];
newItem.itemPage = k;
if (![[noteBookContent object开发者_高级运维AtIndex:(k*3)] isEqualToString:@""]) {
[indexContent addObject:newItem];
}
[newItem release];
}
What will actually happen if indexContent = [[NSMutableArray alloc] init];
is called more than once? If it's bad, how can I prevent this? Should I call this, for instance, in the viewDidLoad? But how would I go about it if I try to do 'lazy-loading', i.e. only allocate indexContent if I really need it? Is there a way to check if it has already been allocated?
I am sorry if all of this is obvious, but I am struggling with this. Perhaps it's a basic concept which I haven't fully grasped yet. Thanks!
EDIT:
I have
@property (nonatomic, retain) NSMutableArray *indexContent; in my header
If you call your function more then once you will leak memory due to the fact that you are not releasing already allocated memory from the previouse call. Simple check would be like this:
if(indexContent)
[indexContent release]
Read memory management docs from apple the will help you a lot.
if (indexContent == nil) indexContent = [NSMutableArray new]; // i screwed up logic first time. derp.
Make sure that when you release indexContent
you set it to nil;
[indexContent release];
indexContent = nil;
(Unless it is the dealloc
method, but probably still a good idea there)
Note that this assumes you want to re-use and potentially further fill the existing array. If not, you could removeAllObjects
or you could release the existing and create anew.
Or, if an @property, you can:
self.indexContent = [NSMutableArray array]; // not +new!!
Or, in that method:
[indexContent release];
indexContent = [NSMutableArray new];
Surround code with a check for nil, if it is nil then allocate the array
//check if it has been allocated
if(indexContent == nil)
{
indexContent = [[NSMutableArray alloc] init];
int numberOfEntries = [noteBookContent count]/3;
for (int k=0; k < numberOfEntries; k++) {
IndexItem *newItem = [[IndexItem alloc] init];
newItem.itemTitle = [noteBookContent objectAtIndex:(k*3)];
newItem.itemPage = k;
if (![[noteBookContent objectAtIndex:(k*3)] isEqualToString:@""]) {
[indexContent addObject:newItem];
}
[newItem release];
}
}
It depends. Is indexContent declared as a retain @property? If so, the runtime will take care of releasing the previous array. If not, and you don't explicitly release it, then yes, it'll leak.
You should also make sure you are releasing indexContext in your dealloc
method.
EDIT: As @bbum mentioned, you'll have to use dot notation. self.indexContent = <whatever>;
My bad for overlooking this.
精彩评论