views:

1264

answers:

3

I am trying to build an array that contains arrays of dictionary objects in Xcode. I can create a working array that has one or more dictionaries in it and then use the addObject: method to add this array as an object in my main array. I cycle around this several times, rebuilding the working array and adding objects to the main array. All good so far, except that on inspecting my main array it contains duplicates of the last dictionaries that the working array was built with. I am assuming this is because when I use the addObject it simply assigns a pointer to the array but does not increase the retain count or create a copy of the working array, hence every time it is rebuilt the main array simply points to the this array.

I guess my question is how do I create a copy of the working array add it to the main array and then rebuild it and add it again? Hope that makes sense and sorry if this appears to be a basic question - it is (I'm new to Xcode) - and any help would be really appreciated.

A: 

I have solved it by re-initialising the inner working array instead of just removing all objects, i.e.,

I did have:

  [sectionArray removeAllObjects];
  [sectionArray addObject:dict];

And changed it to:

  sectionArray  = [[NSMutableArray alloc] init];
  [sectionArray addObject:dict];

I have a feeling though that this is not a good thing to do and I will start to leak memory all over the place.

Dave Hill
Yes it will. You need to release/autorelease it. Or use the NSMutableArray +array method.
nall
It's fine - just make sure you release the old array first, e.g. [sectionArray release] before the above two lines. When you called addObject the retain count on the underlying object will be increased, so releasing sectionArray won't free the actual array. Then when you release the array of pointers it will free all the arrays referenced by those pointers.
Dan J
This is great thanks guys. Am really embarrassed to be asking such basics and this is really helpful.
Dave Hill
This all works, which is great. One thing I don't have my head around yet is, is there a danger that when I am done with the main array and I do a [mainArray release] too that these child arrays will be left behind in memory or will the release destroy them as well?
Dave Hill
These are reference counted. When you alloc it's count is 1. When you release it gets decremented. So you alloc and it's 1. Now you addObject and the main array retains it. Now the count is 2. Now you release your local copy. The count is 1. When the main NSArray is released it releases all its contents. The count is now 0 and it will get collected. No leaks :)
nall
A: 
  [sectionArray release];
  sectionArray  = [[NSMutableArray alloc] init];
  [sectionArray addObject:dict];

This all works, which is great. One thing I don't have my head around yet is, is there a danger that when I am done with the main array and I do a [mainArray release] too that these child arrays will be left behind in memory or will the release destroy them as well?

Dave Hill
You should add this to the original question or read the comments on the answer above. Also you really need to read the objc-c memory management doc http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html
nall
Thanks Nall, on to it. My bad with creating multiple post. New to Stack Overflow too...
Dave Hill
A: 

If all you do with sectionArray is to put it in another array you need to release it after you put it in your mainArray so that only mainArray holds a reference to it:

sectionArray = [[NSMutableArray alloc] init];
[sectionArray addObject:dict];
[mainArray addObject:sectionArray];
[sectionArray release];

When you later release your mainArray reference it will take care of releasing all it's children and your sectionArray's will be freed. (The same goes for the dicts put in sectionArray).

Hope this helped.

m5h