I read that if an object is made using the init it must be released, but if it's something like this elementFormula = [[NSMutableString stringWithString:@""] it is autoreleased. However, there are some other aspects of memory management that confuse me.
For example, something like [[NSMutableString stringWithString:@""] retain] If I retain an autoreleased object, do I have to release it myself, or is it still autoreleased? Also, if I declare a pointer to an object in the .h and assign it a value in the .m like so completeFormula = [self synthesizeFormula]; without going through the init process, do I have to release completeFormula?
Those are my main concerns, but I have posted the entire code below if needed.
@interface EquationTextField : UIView <KeyInput> {
FormulaKeyboard *keyboard;
int consecutiveElementCount;
int subscriptLength;
int chargeIndex;
NSMutableString *elementFormula;
NSString *lastElementPressed;
NSString *charge;
NSString *state;
NSString *completeFormula;
}
init {
NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:@"FormulaKeyboard" owner:self options:nil];
for (id object in bundle) {
if ([object isKindOfClass:[FormulaKeyboard class]])
keyboard = (FormulaKeyboard *)object;
}
self.inputView = keyboard;
keyboard.delegate = self;
elementFormula = [[NSMutableString stringWithString:@""] retain];
charge = [[NSString alloc] initWithString:@""];
state = [[NSString alloc] initWithString:@""];
}
- (void)addElement:(NSString *)currentElement {
if (![currentElement isEqualToString:lastElementPressed]) {
// when new element is pressed
[elementFormula appendString:currentElement];
lastElementPressed = currentElement;
consecutiveElementCount = 1;
}
else if ([currentElement isEqualToString:lastElementPressed]) {
// when element is pressed consecutively
if (consecutiveElementCount == 1) {
// since there is no '1' subscript, nothing needs to be deleted
[elementFormula appendString:@"₂"];
subscriptLength = 1;
}
if (consecutiveElementCount > 1) {
// deletes last subscript and replaces it with new subscript
NSArray *subscriptList = [[[NSArray alloc] initWithObjects:@"₂", @"₃", @"₄", @"₅", @"₆", @"₇", @"₈", @"₉", @"₁₀", @"₁₁", @"₁₂", nil] autorelease];
NSRange range = NSMakeRange (([elementFormula length] - subscriptLength), subscriptLength);
[elementFormula deleteCharactersInRange:range];
NSString *tempSubscript = [subscriptList objectAtIndex:consecutiveElementCount - 1];
[elementFormula appendString:tempSubscript];
subscriptLength = [tempSubscript length];
}
if (consecutiveElementCount < 11)
consecutiveElementCount ++;
}
completeFormula = [self synthesizeFormula];
}
- (void)changeCharge:(NSString *)chargeIncrease {
NSArray *chargeList = [[[NSArray alloc] initWithObjects:@"⁴⁻", @"³⁻", @"²⁻", @"⁻", @"", @"⁺", @"²⁺", @"³⁺", @"⁴⁺", nil] autorelease];
if ([chargeIncrease isEqualToString:@"+"]) {
chargeIndex += 1;
charge = [chargeList objectAtIndex:chargeIndex];
}
else if ([chargeIncrease isEqualToString:@"-"]) {
chargeIndex -= 1;
charge = [chargeList objectAtIndex:chargeIndex];
}
completeFormula = [self synthesizeFormula];
}
- (void) changeState:(NSString *)stateName {
state = stateName;
completeFormula = [self synthesizeFormula];
}
- (NSString *)synthesizeFormula {
NSMutableString *synthesizedFormula = [[[NSMutableString alloc] initWithString:elementFormula] autorelease];
[synthesizedFormula appendString:charge];
[synthesizedFormula appendString:state];
return synthesizedFormula;
}
- (void)dealloc
{
[charge release];
[state release];
self.inputView = nil;
keyboard.delegate = nil;
[keyboard release];
[super dealloc];
}