Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kristiaand

macrumors member
Original poster
Dec 5, 2007
63
0
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?
 

kristiaand

macrumors member
Original poster
Dec 5, 2007
63
0
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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

kristiaand

macrumors member
Original poster
Dec 5, 2007
63
0
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

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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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.

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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.