I'm relatively new to Objective C and while I've been trucking right along writing iPhone apps, I think I need some help understanding something.
I'm storing a dynamic list of strings in an NSMutableArray, which is a property of my main class. In my initialization I alloc and init to store 100 items:
stringList = [[NSMutableArray alloc] initWithCapacity:100];
As strings are acquired (from incoming data) I add them as objects to the array:
NSString *newString = [stringMaker makeStringFromData:data];
[stringList addObject:newString];
So to understand what's really happening here. If I've done this adding 5 times, what do I really have in the NSMutableArray? Just pointers to the 5 strings or the actual strings? I had assumed that addObject actually copied the data. If my stringMaker class were to start properly releasing things could I end up with pointers to nothing? Is there a better way to make sure that the strings in my list are safe from being released or repointed to something else? Or am I misunderstanding how this works?
I'm not currently having a problem with this part so far. But I'm a little shaky on leaks and releasing things.
What lead me to this is that I do have a problem where when I modify one of the strings by:
[stringList replaceObjectAtIndex: stringIndex withObject: stringMaker.lastStringUsed];
that after a while when I come back to access the object at that index, it's whatever lastStringUsed was last, not what it was when I replaced it. This indicates to me that all I did was set a pointer and didn't save the data at that pointer. Note that lastStringUsed is a property of the stringMaker class and is an NSMutableString.
So to be clear.. in stringMaker at various points I'm doing a:
[lastStringUsed setString: someString];
(by the way does THAT actually move the data?)
Then in back in my main class I'm doing that replaceObjectAtIndex shown above. Then some other stringMaker method is resetting lastStringUsed. Then later when I access my stringList at that index it's the new lastStringUsed value and not whatever it was when I did the replaceObjectAtIndex.
EDIT - SOLVED FOR NOW - STILL NEED TO TAKE A NIGHT CLASS ON NSARRAYS AND STRINGS AND PROPERLY RETAINING AND RELEASING
What I did to fix it for now was to just allocate a new string...
NSString *replacementString = [[NSString alloc] initWithCapacity: 310];
[replacementString setString:stringMaker.lastStringUsed];
[stringList replaceObjectAtIndex:stringIndex withObject:replacementString];
Now the next time I change lastStringUsed it doesn't also change the one in the stringList.