views:

263

answers:

3

Hi,

I'm constructing a small iphone app and using a singleton to store and update a string that gets updated when the user taps letters or numbers on the screen to form a code.

i.e. they tap 3 then S then 4 and I need to track and combine that input to give me "3S4" say. When the singleton is initialised it creates an empty NSString and I then use the stringByAppendString method to add on the next letter/number tapped. When I first tried this I did not have the [enteredCode retain] line in there and the app would crash with EXC_BAD_ACCESS, always after 2 inputs. I set the NSZombie property which told me that the enteredCode had been de-allocated but I don't know where or how that happened. All I know is that at the end of the addInput method it will report the retainCount to be 2 say and then straight after I can see (by calling the singleton from elsewhere) it will drop down to 1 (when the retain line is in there).

My question is: though what I've done by adding [enteredCode retain] works for me am I breaking some rules here or going about this in the wrong/bad way? I just can't see why the string is being released.

I'm new to Objective-C btw

in MySingleton.h

@interface MySingleton : NSObject {
   NSString *enteredCode;
}   

in MySingleton.m

-(void) addInput:(NSString *) input
{ 
  NSLog(@"enteredCode retain count is : %d \n ",[enteredCode retainCount]);

  enteredCode = [enteredCode stringByAppendingString:input];

 NSLog(@"enteredCode retain count is : %d \n ",[enteredCode retainCount]);

 [enteredCode retain]; // without this the app crashes

 NSLog(@"enteredCode retain count is : %d \n ",[enteredCode retainCount]);

}

-(id) init
{
    self = [super init];

    if (self)
    {
        enteredCode = @"";


    }

    return self;
}
+3  A: 

First, never use the -retainCount method. The absolute count of retains on an object is an implementation detail of the frameworks and will often return confusing results.

Retain counts are something you should maintain entirely as a balanced set of deltas. If you cause a retain count to be added to something, you must release or autorelease that object somewhere. End of story.

This document explains it all.

With that knowledge in hand, the source of your crash is a fairly common memory management mistake.

  enteredCode = [enteredCode stringByAppendingString:input];

Every time that line of code is executed, you are replacing enteredCode with an autoreleased instance of NSString. The autorelease pool is drained and your program crashes the next time enteredCode is used.

Your solution of retaining enteredCode is only half the solution. You need to ensure that the original value of enteredCode is released, too. See the memory management docs.

If this were my app, I would turn enteredCode into an @property that copies the string and always set and access enteredCode through that property, never retaining or releasing it manually in my code (outside of -dealloc, of course).

bbum
Many thanks for clearing that up. Thanks for everyone else's response too. I have altered the code now to set enteredCode as an @property.
Cerv
+1  A: 

NSString's stringByAppendingString: returns a new NSString made by appending one string to the other, and the new NSString is set to autorelease, which empties the autorelease pool and your next run crashes the app. You're redefining an existing string with stringByAppendingString:, and that's causing the retain problems. (Alternatively, use NSMutableString and you can avoid this.)

By the way, you can do if (self = [super init]) in your init override. The declaration returns true if it occurs or can occur.

Arseniy Banayev
So basically what the original questioner is doing is correct, by retaining the string after making a new appended instance. As for (self = [super init]), what the original poster is doing seems to be the direction Apple is headed. If you use the "Create Init Method" macro in XCode "self = [super init]; if (self) ..." is what is produced.
Kendall Helmstetter Gelner
+2  A: 

Here's how your code should look:

@interface MySingleton : NSObject {
    NSString *enteredCode; 
}

@property (nonatomic, retain) NSString *enteredCode;

@end


@synthesize enteredCode;

-(void) addInput:(NSString *) input
{ 
    self.enteredCode = [self.enteredCode stringByAppendingString:input];
}

- (void)dealloc {
    [enteredCode release];
}

@end
kubi
I suggest making `enteredCode` `nonatomic`, too. The `init` should also be changed to `self.enteredCode = @"";`, as you don't want to release a string literal.
MrMage
Releasing a string literal is fine, it's no different than releasing any other NSString.
kubi
Of course, but it never gets retained, so you shouldn't have to (and shouldn't) release it.
MrMage