views:

82

answers:

3

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";

}
+1  A: 

You need to retain it: otherwise it will be released at the end of the current scope, as stringByAppendingString returns an autoreleased object.

Please read the cocoa memory management guide.

chpwn
+2  A: 

Cocoa memory management doesn't involve scope. Whether an object lives or dies is determined by ownership — if another object owns it (by having created it with alloc, new or copy, or claiming ownership with retain), it will stay around. When an object doesn't have any more owners, it can go away. In your case, you want to keep that NSString object around in fullCmdString, but you don't do anything to claim ownership of it, so it just silently goes away and becomes a garbage pointer.

I'd strongly suggest reading Apple's memory management rules. It's a very simple set of guidelines, but it makes all the difference between a working program and one with weird bugs like this.

Chuck
Not absolutely true. Your initial claim assumes all other code in the app is properly behaved. It's quite possible for an object owned/retained by one object to be (over)released by another buggy object which may or may not own it as well. Realizing this is a necessity when debugging code of unknown quality (e.g. maybe your own when coding late at night after a few too many beers :).
hotpaw2
@hotpaw2: That doesn't contradict anything I said. If somebody's overreleasing it, that violates the memory management contract.
Chuck
A: 
dreamlax
That makes sense about the run loop auto-releasing the string. I think I'm going to curl up with the Apple memory-management docs this weekend. Thank you!
John