NSMutableArray objects do not call the dealloc method after removeAllObjects

I am trying to run the dealloc method for some elements that / were stored in a mutable array but cannot find a way to do this.

I ran into this problem when working in a larger ARC project and found the answer in this post: The dealloc method is not called when the object is set to zero . After reading this answer, I felt that I understood how ARC should process this code (see below), however, after starting in a very simple test project, I get the same results.

In the main view controller, I run the modified array (strong property) and add some other view controllers to it. Then I delete all the objects:

- (void)viewDidLoad{ [super viewDidLoad]; containerArray = [[NSMutableArray alloc]init]; for(int i = 0; i < 10; i++){ //item +1 (item at +1) Item *item = [[Item alloc]initWithNibName:nil bundle:nil]; //item +1 (item at +2) [containerArray addObject:item]; //ARC should release item -1 (item at +1...I think) } //removeAllObjects should release each item -1 (item(s) at 0) [containerArray removeAllObjects]; //dealloc should be called... } 

In the item view controller:

 -(void)dealloc{ NSLog(@"item dealloc"); } 

Any help is greatly appreciated.

+4
source share
1 answer

After reading your code 3 times, I did not see any flaws in your approach. I thought that everything was correct and that dealloc should be called 10 times when you delete all objects from the array.

Then I decided to try the code, and I just found that ... we were both right :) the code is perfect. When I run it, I get the dealloc output of the element 10 times.

  • Are you sure you are trying to use it in an ARC project?
  • Is dealloc inside the class Item ?

There is one more thing that you are not doing right, but the code and arguments are correct.

+2
source

All Articles