1

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.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Jeremy
  • 883
  • 1
  • 20
  • 41

6 Answers6

2

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.

sergio
  • 68,819
  • 11
  • 102
  • 123
2

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.

bosmacs
  • 7,341
  • 4
  • 31
  • 31
1

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.

Jacob Gorban
  • 1,431
  • 1
  • 9
  • 15
  • In the end yes, 10, but I wanted to signify that the last instantiation overwrites the previous 9 (they leak) and after the last allocation only the 10th is still living. – Jacob Gorban Aug 04 '11 at 19:43
  • How would things change, if at all, if I autoreleased i.e. as loop but calling NSMutableArray *myArray = [[[NSMutableArray alloc] init]autorelease]? I've never understood when the iOS knows when to (auto)release an object. – Jeremy Aug 05 '11 at 19:16
  • Then all 10 would be released at the end of the run loop. The framework had an AutoRelease pool around every run of the run-loop. So at the end of all your code of one run-loop, all autoreleased objects will be released. That being said, you can created your own, autorelease pools if you expect your code to auto-release a lot of objects. Especially in such case as here, when you autorelease in a loop. If you had not 10 but 1,000,000 objects in a loop, it would be a good idea to put an autorelease pool inside the loop to keep the memory from growing. – Jacob Gorban Aug 06 '11 at 08:01
1

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.

Marsson
  • 687
  • 7
  • 16
0
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.

Chris Frederick
  • 5,482
  • 3
  • 36
  • 44
Praveen-K
  • 3,401
  • 1
  • 23
  • 31
0

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.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281