简体   繁体   English

防止多次分配同一个数组

[英]Preventing to alloc the same array more than once

A beginner's problem: I have a method which puts data into a MutableArray.初学者的问题:我有一种将数据放入 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:潜在地,这个方法可以被多次调用,我有点担心它会泄漏 memory 因为我在每次调用它时分配数组:

    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];
}

What will actually happen if indexContent = [[NSMutableArray alloc] init];如果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?例如,我应该在 viewDidLoad 中调用它吗? But how would I go about it if I try to do 'lazy-loading', ie only allocate indexContent if I really need it?但是,如果我尝试进行“延迟加载”,即仅在我真的需要时才分配 indexContent,我将如何处理它? 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; @property (nonatomic, 保留) NSMutableArray *indexContent; in my header在我的 header

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时将其设置为 nil;

[indexContent release];
indexContent = nil;

(Unless it is the dealloc method, but probably still a good idea there) (除非它是dealloc方法,但在那里可能仍然是一个好主意)

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.如果没有,您可以removeAllObjects或者您可以释放现有的并重新创建。


Or, if an @property, you can:或者,如果是@property,您可以:

self.indexContent = [NSMutableArray array]; // not +new!!

Or, in that method:或者,在该方法中:

[indexContent release];
indexContent = [NSMutableArray new];

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.如果您调用 function 的次数更多,那么您将泄漏 memory,因为您没有从先前的调用中释放已分配的 memory。 Simple check would be like this:简单的检查是这样的:

if(indexContent)
  [indexContent release]

Read memory management docs from apple the will help you a lot.阅读 Apple 的 memory 管理文档会对您有很大帮助。

Surround code with a check for nil, if it is nil then allocate the array环绕代码检查 nil,如果它是 nil,则分配数组

//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? indexContent 是否声明为保留@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.您还应该确保在您的dealloc方法中释放 indexContext。

EDIT: As @bbum mentioned, you'll have to use dot notation.编辑:正如@bbum 提到的,你必须使用点符号。 self.indexContent = <whatever>; My bad for overlooking this.忽略这一点是我的坏事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM