I have an object that is appending a local string to another, and then handing that to an instance variable. This StringObj is being held by the calling UITableViewController. On the first call, fullCmdString has the string correctly. However, on 2nd call, the fullCmdString is null, or full of weird data (looks like data in odd mem location). My best guess is that stringByAppendingString is returning a reference to inStr, and not a new NSString object, so the data is going out of scope. Can anyone shed some light on this?
#import "StringObj.h"
@synthesize fullCmdString;
- (void)assemble:(NSString *)inStr {
NSString *add = @"text added";
//doesn't hold onto string
fullCmdString = [NSString stringWithString:[inStr stringByAppendingString:add]];
//doesn't hold onto string either
fullCmdString = [inStr stringByAppendingString:add];
//works great
fullCmdString = @"basic text text added";
}
An autoreleased string gets sent a
releasemessage at the end of the run loop iteration (along with everything else in the autorelease pool). If you want it to survive the run loop iteration, avoid putting it in the autorelease pool, or explicitly retain it:See the Cocoa memory management rules for more information. This guide will tell you that anything that you’ve explicitly allocated or retained—like the above—you will also have to explicitly release when you are done with it.