如果在循环中分配,会创建多少个对象

发布于 2024-11-27 18:07:27 字数 305 浏览 1 评论 0原文

我正在尝试了解 iPhone SDK 中内存管理的一个方面。

如果我运行:

for (int x = 0; x < 10; x++)  {
    NSMutableArray *myArray = [[NSMutableArray alloc] init];
}

我是在内存中创建 10 个 myArray 对象,还是每个分配都会覆盖前一个?如果是后者,我想我只需要在循环后进行一次 [myArray release] 即可进行清理。如果是前者,我想我需要在 For 循环内释放。

谢谢。

I'm trying to get my mind around one aspect of memory management in the iPhone SDK.

If I ran:

for (int x = 0; x < 10; x++)  {
    NSMutableArray *myArray = [[NSMutableArray alloc] init];
}

Am I creating 10 myArray objects in memory, or does each alloc overwrite the previous? If the latter, I presume I would only need one [myArray release] after the loop to clean up. If the former, I presume I need the release inside the For loop.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

南薇 2024-12-04 18:07:27

您会获得十种不同的分配,如果您不希望内存泄漏,则需要释放它们。

 for (int x = 0; x < 10; x++)  {

    NSMutableArray *myArray = [[NSMutableArray alloc] init];
    ....
    [myArray release];
 }

如果不释放,泄漏的对象实际上是 10,而不是注释中的 9,因为在循环之外,您无权访问循环局部变量,并且最后分配的对象也将无法访问。

You get ten different allocations and you need to release them if you do not want memory leaks.

 for (int x = 0; x < 10; x++)  {

    NSMutableArray *myArray = [[NSMutableArray alloc] init];
    ....
    [myArray release];
 }

If you don't release, the leaked object would actually 10, not 9 as per comment, since outside of the loop you don't have access to the loop local variable and the last allocated object would also be unreachable.

那伤。 2024-12-04 18:07:27

实际上,您有 10 个对象有 10 个泄漏。一旦离开循环,myArray 就不再在范围内(因此无法访问),因此也无法释放第 10 个分配。

Actually, you have 10 objects with 10 leaks. Once you leave the loop, myArray is no longer in scope (and is therefore inaccessible), so there is no way to release the 10th allocation either.

骷髅 2024-12-04 18:07:27

您正在创建 10 个对象,其中 9 个已泄漏。
在循环结束时使用它们后应该释放。

这也不仅仅是关于iPhone SDK。这是基本的 Cocoa 内存管理。也适用于 Mac。

You're creating 10 objects, 9 of which are leaked.
You should release after you use them in the end of the loop.

This also not only about iPhone SDK. It's basic Cocoa memory management. Also works on the Mac.

书间行客 2024-12-04 18:07:27

在这种情况下,您将创建 10 个不同的 Array 对象,每个对象的保留计数为 1,并且没有任何引用。这将是一个“内存泄漏工厂”,其中 10 个对象永远不会从内存中释放。 :)

哎呀...没有看到发布...9 个泄露的数组。

In that case you are creating 10 different Array objects each one with a retain count of 1, and no reference whatsoever. This would be a "memory leak factory" with the 10 objects never beign released from the memory. :)

oooops... did not see the release...9 leaked arrays.

浪漫之都 2024-12-04 18:07:27

除了大家(正确地)所说的之外,Cocoa 还支持自动释放对象。如果您这样改写您的代码片段:

for (int x = 0; x < 10; x++)
{      
    NSMutableArray *myArray = [NSMutableArray arrayWithObjects: ...];     
    //....     
} 

您仍然分配 10 个不同的数组,但没有被泄漏。它们最终会自动释放。

In addition to what everyone (rightly) said, Cocoa also supports autoreleased objects. If you rephrase your snippet thus:

for (int x = 0; x < 10; x++)
{      
    NSMutableArray *myArray = [NSMutableArray arrayWithObjects: ...];     
    //....     
} 

you still allocate 10 different arrays, but none are leaked. They are autoreleased eventually.

-小熊_ 2024-12-04 18:07:27
for (int x = 0; x < 10; x++)  {

    NSMutableArray *myArray = [NSMutableArray array]; //Its an autorelease
    ....
}

这将创建 10 个不同的 NSMutableArray 对象。您实际上不需要显式释放它们。myArray 在运行循环结束时自动释放。

如果您使用名称以“alloc”或“new”开头或包含“copy”的方法创建对象(例如,allocnewObject),则您将获得该对象的所有权,或 mutableCopy),或者如果您向其发送保留消息。您有责任使用 releaseautorelease 放弃您拥有的对象的所有权。任何其他时候您收到对象时,都不得释放它。

NSMutableArray *myArray = [NSMutableArray array]; 中,您不拥有该数组的所有权,它会自动释放给您。

您可以在此处了解有关内存管理的更多信息。

for (int x = 0; x < 10; x++)  {

    NSMutableArray *myArray = [NSMutableArray array]; //Its an autorelease
    ....
}

This creates 10 different NSMutableArray objects. You actually do not need to release them explictly.myArray is autoreleased at the end of the run loop.

You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message. You are responsible for relinquishing ownership of objects you own using release or autorelease. Any other time you receive an object, you must not release it.

In NSMutableArray *myArray = [NSMutableArray array];, you do not take ownership of the array, and it will be passed to you autoreleased.

You can learn more about memory management here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文