macos NSMutableArray 内存管理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/456233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:00:54  来源:igfitidea点击:

NSMutableArray memory management

iphonecocoamacos

提问by chicken

NSMutableArray *a1 = [[NSMutableArray alloc] init];
NSMutableArray *a2 = [NSMutableArray array];

TempObj *obj = [[TempObj alloc] init]; //assume this line is repeated for each obj
[a1 addObject:obj];
[a1 addObject:obj2];
[a1 addObject:obj3];
[a1 addObject:obj4];

[obj release];
[obj2 release];
[obj3 release];
[obj4 release];

[a1 release];

Ok so a2 is an autorelease obj so I don't have to call release on it? Also how do you know when you get an autorelease object?

好的,所以 a2 是一个自动释放 obj,所以我不必在它上面调用 release?另外你怎么知道你什么时候得到一个自动释放对象?

And for a1, I don't have to loop through the array and release each object first? What if I called [a1 removeAllObjects]; does that call [[a1 objectAtIndex:#] release];

而对于 a1,我不必遍历数组并先释放每个对象?如果我调用 [a1 removeAllObjects]; 是否调用 [[a1 objectAtIndex:#] release];

Am I supposed to release those objects after I've added them to the array?

我应该在将它们添加到数组后释放这些对象吗?

回答by Andy

When you add an object to an array, it calls retainon that object. If you don't release your pointer to that object, it will be a leak. When you release the array, it will call releaseon all of the objects that it holds, since it called retainpreviously.

当您将对象添加到数组时,它会调用retain该对象。如果您不释放指向该对象的指针,则会发生泄漏。当您释放数组时,它将调用release它持有的所有对象,因为它retain之前调用过。

As for autorelease vs release, the only way to know for sure (aside from possibly reading the documentation) is by the name of the method. I believe the rule in general is that if you didn't allocate the object, then you aren't responsible for releasing it.

至于 autorelease 与 release,唯一确定(除了可能阅读文档)的方法是通过方法的名称。我相信一般的规则是,如果您没有分配对象,那么您无需负责释放它。

Regarding the object creation methods, all of the convenience methods (array:, arrayWithObjects:, arrayWithArray:, etc.) return autoreleased objects. However, their corresponding init methods (init:, initWithObjects:, initWithArray:, etc.) do not - if you call them, you are responsible for calling releaseon the returned object.

关于所述对象创建方法中,所有的方便的方法(array:arrayWithObjects:arrayWithArray:等等)返回自动释放物体。但是,它们对应的 init 方法(init:initWithObjects:initWithArray:等)不会 - 如果您调用它们,则您负责调用release返回的对象。

I seem to recall a few other questions on this topic here - you might try searching around for a more thorough explanation.

我似乎在这里想起了关于这个主题的其他一些问题——你可以尝试四处寻找更彻底的解释。

回答by Chuck

It's easier to think of Objective-C memory management as ownershiprather than in terms of retain-release. When you add the objects to the array, the array is now a co-owner of the object and is responsible for properly managing the memory. When the owner (whatever object contains the code you posted) calls releaseon the objects, it's giving up ownership and now the array is the sole owner.

将 Objective-C 内存管理视为所有权而不是保留释放更容易。当您将对象添加到数组时,数组现在是对象的共同所有者,并负责正确管理内存。当所有者(任何包含您发布的代码release的对象)调用这些对象时,它就放弃了所有权,现在该数组是唯一的所有者。

Apple has a good primer on how ownership works in Cocoa (including how you know when you are responsible for calling releaseon an object): Memory Management Programming Guide For Cocoa. It's a must-read if you want to use Cocoa.

Apple 有一本关于 Cocoa 中所有权如何工作的很好的入门书(包括您如何知道何时负责调用release对象):Memory Management Programming Guide For Cocoa。如果您想使用 Cocoa,这是必读的。

回答by Jason Harris

The basic thing to remember is this: You must balance every call to "init", "retain" or "copy" with a corresponding call to "release" or "autorelease". That's really all that you need to know.

要记住的基本事情是:您必须平衡对“init”、“retain”或“copy”的每次调用与对“release”或“autorelease”的相应调用。这就是您需要知道的全部内容。

In your example, a1 had a call to "init", so you need to have a "release" somewhere on it. Ditto with "obj". You didn't call "init", "retain", or "copy" on anything else, so you don't need to call "release" or "autorelease" on anything else.

在你的例子中,a1 调用了“init”,所以你需要在它的某个地方有一个“release”。同上“obj”。你没有在其他任何东西上调用“init”、“retain”或“copy”,所以你不需要在其他任何东西上调用“release”或“autorelease”。

回答by Ahmad Ashraf bin Azman

i dont know either im right or wrong

我不知道我对还是错

NSMutableArray *a1 = [[NSMutableArray alloc] init]; // will be autorelease
NSMutableArray *a1 = [NSMutableArray alloc]; // will not autorelease

after you [a1 removeAllObjects];

您先请 [a1 removeAllObjects];

回答by McLas

You released obj, obj2, obj3, obj4 just after adding them to the array(best way). Otherwise you'd have to loop and release each object (that's the stupid way : the one I've done till now).

您在将 obj、obj2、obj3、obj4 添加到数组后(最佳方式)释放了它们。否则你必须循环并释放每个对象(这是愚蠢的方式:我到目前为止所做的)。