loop, objects and array wierdness

Discussion in 'Mac Programming' started by kristiaand, Nov 6, 2008.

  1. kristiaand macrumors member

    Joined:
    Dec 5, 2007
    #1
    Hi everyone,
    after some advice with a bit of code i have which seems to be going wrong somewhere but i cannot spot it.

    heres the code and below is my guess at whats going wrong with it, can someone let me know where i am going wrong?

    Code:
    int n ;
    	NSMutableString *finalStringValue =[[NSMutableString alloc] init];
    	for (n = 1; n <= 10; n++)
    	{
    		TableData *dataObject = [[TableData alloc] init];
    		
    		[finalStringValue setString:@"hello "];
    		[finalStringValue appendFormat:@"%d",n];
    		
    		[dataObject setCity:finalStringValue];
    		[dataObject setNickname:@"Big Apple"];
    
    		[tableArray addObject: dataObject];
    		
    		[dataObject release];
    		
    		NSLog(finalStringValue);
    	}
    	[finalStringValue release];
    From what i can guess im using the object and assigning it to the array and even though i am (assuming i am) releasing it when it goes through the loop again and creates a new one, its being "reconnected" to the one in the array...

    what am i doing wrong?
     
  2. kristiaand thread starter macrumors member

    Joined:
    Dec 5, 2007
    #2
    as normal after i post something on here i work out whats going wrong. i moved the alloc > init code for the string and the release inside the loop and my problem went away.

    i used the debugger (big step for me) and noticed that the strings memory address was the same, so figured it was not being released and just being overwritten all the time (remembering that the array stores a pointer to it, is that right?)

    ne way i got it working !! im getting better.
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    finalStringValue is used in the setCity call of EVERY data object. It is mutable, so you modify what is "in" finalStringValue in every iteration, but it's always the same object.

    You may want to simply use an NSString * inside the loop, and set it using -initWithFormat. You can release this at the end of the loop, as long as setCity willl retain it. I don't think TableData is a standard class, so I have no idea. If setCity does not retain the NSString passed to it... it should.

    -Lee
     
  4. kristiaand thread starter macrumors member

    Joined:
    Dec 5, 2007
    #4
    hi lee thanks for the reply, setCity does a retain and release, so that should be fine. can you be a bit more specific on how to use NSString in that code as i am still learning my way around Cocoa and every time i try so far i get errors during the compile process.
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    Code:
            int n;
    	for (n = 1; n <= 10; n++)
    	{
    		TableData *dataObject = [[TableData alloc] init];
    		NSString *myCityString = [[NSString alloc] initWithFormat:@"hello %d",n];
    		[dataObject setCity:myCityString];
                    [myCityString release];
                    [dataObject setNickname:@"Big Apple"];
    
    		[tableArray addObject: dataObject];
    		
    		[dataObject release];
    		
    	}
    
    Unless setCity takes a NSMutableString, this should work fine for what you're doing. Using a mutable string is fine here, but not really necessary. The big part is releasing in the loop and getting a new object each time, which you already figured out yourself.

    -Lee
     

Share This Page