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;
}
First, never use the
-retainCountmethod. 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
releaseorautoreleasethat 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.
Every time that line of code is executed, you are replacing
enteredCodewith an autoreleased instance of NSString. The autorelease pool is drained and your program crashes the next timeenteredCodeis used.Your solution of retaining
enteredCodeis only half the solution. You need to ensure that the original value ofenteredCodeis released, too. See the memory management docs.If this were my app, I would turn
enteredCodeinto an @property that copies the string and always set and accessenteredCodethrough that property, never retaining or releasing it manually in my code (outside of-dealloc, of course).