开发者

How many objects are created if alloc'ed in a loop

开发者 https://www.devze.com 2023-03-25 03:55 出处:网络
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++){

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 [开发者_JAVA技巧myArray release] after the loop to clean up. If the former, I presume I need the release inside the For loop.

Thanks.


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.


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.


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消