iPhone Test Leaks
Categories: iPhone on Nov.12, 2009
Test leaks NSMutableArray. Release object with objects
В обоих примерах все впорядке:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | - (IBAction) startTestString { if (testArr == nil) { testArr = [[NSMutableArray alloc] init]; } for (int i = 0; i < 100; i++) { [testArr addObject:@"Test text!"]; } [testArr removeAllObjects]; [testArr release]; testArr = nil; } // В этом примере показывается, что если вы не сделали release, то за вас его никто не сделает - (IBAction) startTestImage { if (testArr == nil) { testArr = [[NSMutableArray alloc] init]; } for (int i = 0; i < 100; i++) { NSString *fileLocation = [[NSBundle mainBundle] pathForResource:@"image.jpg" ofType:nil]; UIImage *image = [[UIImage alloc] initWithContentsOfFile:fileLocation]; [testArr addObject:image]; [image release]; } NSLog(@"%d", [testArr count]); [testArr removeAllObjects]; [testArr release]; testArr = nil; } |
To add an element at the end of the array, you can use addObject, as in:
NSMutableArray *array;
array = [NSMutableArray new];
[array addObject: anObject];
Assuming anObject is an NSObject (but not nil, remember, you can’t put a nil object into an NSArray). As usual, anObject is RETAINed when it is added to the array.
If you want to insert an object into an array at a certain position, you can use insertObject:atIndex::
NSMutableArray *array;
array = [NSMutableArray new];
[array addObject: @"Michele"];
[array addObject: @"Nicola"];
[array insertObject: @"Alessia" atIndex: 1];
To remove an object, you can use removeObjectAtIndex:, as in
NSMutableArray *array;
array = [NSMutableArray new];
[array addObject: @"Michele"];
[array addObject: @"Nicola"];
[array insertObject: @"Alessia" atIndex: 1];
/* Now the array contains Michele, Alessia, Nicola. */
[array removeObjectAtIndex: 0];
/* Now the array contains Alessia, Nicola. */
When an object is removed from the array, it receives a release message. This balances the retain which was sent to the object when it was first added to the array, and allows the object to be deallocated, if needed.
Similar posts:

November 13th, 2009 on 1:24 pm
при добавлении объекта в массив ему посылается сообщение retain, поэтому за вас ваши alloc’и никто не зарелизит
November 13th, 2009 on 1:34 pm
да, вы правы, но когда объект удаляется из массива ему делается release
добавлю немного текста для ясности
November 13th, 2009 on 2:23 pm
если во втором примере не сделать
[testArr removeAllObjects];
то будет утечка памяти
November 13th, 2009 on 4:33 pm
Почему то везде пишут: \"When the array itself is deallocated, it will send a release to each of its items\", – что вобщем и подтверждает следующий код:
NSMutableArray *array = [[NSMutableArray alloc] init];
NSNumber *numb = [[NSNumber alloc] initWithInt:100];
NSLog(@\"1: %d\", [numb retainCount]); // 1
[array addObject:numb];
NSLog(@\"2: %d\", [numb retainCount]); // 2
[array removeAllObjects];
[array release];
NSLog(@\"3: %d\", [numb retainCount]); // 1 [array release] – послал своему //елемунту релиз
[numb release];
ликов не обнаружено.
November 13th, 2009 on 4:36 pm
все правильно
November 13th, 2009 on 4:44 pm
Ой, прошу прощения, в коде нада убрать [array removeAllObjects].
December 3rd, 2009 on 12:43 pm
так я что-то так и не понял
[array removeAllObjects]
оставлять или убирать?
December 3rd, 2009 on 2:48 pm
лучше оставлять!
December 3rd, 2009 on 2:57 pm
значит правильно делал :)
спасибо!