1

I have data in an NSMutableString, localPName. I want to save the user’s data like so:

[[NSUserDefaults standardUserDefaults] setObject:aString forKey:@"thePNameKeyValue"];

The compiler is trying ever so hard to teach me about “message sent to deallocated instance.” Other than synthesize and a single use (which prints valid data), I don’t see where localPName was touched, much less “deallocated,” released, whatever.

NSMutableString *localPName;
NSString        *whateverNSString;
@property(nonatomic,retain) NSMutableString *localPName;
@property(nonatomic,retain) NSString *whateverNSString;


@synthesize localPName;
@synthesize whateverNSString;
localPName = [[NSMutableString alloc] initWithCapacity:40];
//  [localPName appendString:@""];  ← seems to make no difference

- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSMutableString *teststring=[NSMutableString stringWithString:textField.text];
    localPName = teststring;
    NSLog(@"at textFieldDidEnd, localPName='%@'",localPName);   // ← prints correct data
}

From here on out I'm pretty much out to lunch.

To respond to “What have you tried?” I’ve included many of the suggestions I’ve cadged from the ‘net. How to initialize NSString to NSMutableString? Objective-C "message sent to deallocated instance 0x5633b0" The results from above were: (gdb) info symbol 0x2f699fe0 No symbol matches 0x2f699fe0.

//  1
//NSString *str1 = @""; 
//  str1 = [str1 stringByAppendingString:localPName];
//  2
//  NSString *str1 = [[NSString alloc] initWithString:localPName];
//  3
//  whateverNSString = [[NSString alloc] initWithString:localPName];
//  4
//  whateverNSString =localPName;   //  message sent to deallocated instance, nonatomic,retain
//  5
//  self.whateverNSString =localPName;  //  message sent to deallocated instance, nonatomic, retain
//  6
//  whateverNSString =localPName;   //  message sent to deallocated instance, nonatomic, retain
//  7
//  glbl_asNeededInt = [whateverNSString length];   //  message sent to deallocated instance, nonatomic, copy
//  NSLog(@"saveButton 3 length=%d",glbl_asNeededInt);
//  8
//  whateverNSString = localPName;  //  crash. no crash log, no console. no nuthin' 
//  9
//  https://stackoverflow.com/questions/1354490/how-to-take-an-nsstring-out-of-an-nsmutablestring
//  whateverNSString = [localPName copy];
//  10
//  whateverNSString = (NSString*) localPName;
//  11
//  NSString* aString = [NSString stringWithFormat: @"%@", localPName]; // message sent ...
//  12
// NSString* aString = [NSString stringWithString:localPName];  // message sent ...

Holidays are coming up. My thanks to the folks that just might contribute to my getting a gift or two early.

Community
  • 1
  • 1
d_CFO
  • 85
  • 1
  • 2
  • 8

1 Answers1

2

testString points to an autoreleased instance which you assign directly to instance variable. You should use the setter instead:

self.localPName = teststring;

With that you take ownership of the string (the setter takes ownership by retaining the instance) and you also don't leak the instance you assigned to it previously (it will be released properly by the setter).

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236