1

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.


        [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?


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.

7
  • when you do addObject in an array, i blive the array retains this object... so your assumption that this d oes not happen is wrong Commented Nov 19, 2009 at 19:01
  • 1
    Code sample? What you're asking for is possible, so you may have a bug. Commented Nov 19, 2009 at 19:02
  • Yes it will. You need to release/autorelease it. Or use the NSMutableArray +array method. Commented Nov 19, 2009 at 19:07
  • 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. Commented Nov 19, 2009 at 19:08
  • This is great thanks guys. Am really embarrassed to be asking such basics and this is really helpful. Commented Nov 19, 2009 at 19:09

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.