HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Am I managing my memory correctly?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
correctlymanagingmemory

Problem

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 {

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

Solution

It's not init that causes you to have to be responsible for releasing the memory its alloc. If you call alloc, retain or new (this is rarely used these days) then you are responsible for releasing the memory yourself.

So in the case where you had an autoreleased object and you retained it. Then yes you would then be responsible for subsequently releasing it.

The most common side affect where you can be responsible for releasing an object you didn't explicitly call alloc or retain on is with a property that has the retain attribute.

@property (retain) NSString *string;

if you then have the following code somewhere

self.string = [NSString stringWithString:@"This is an autoreleased string typically"];


calling the self.string piece will cause it to retain the value you are setting it with. So you would want to make sure and have the following to clean it up, probably in your dealloc.

self.string = nil;

Code Snippets

self.string = [NSString stringWithString:@"This is an autoreleased string typically"];
self.string = nil;

Context

StackExchange Code Review Q#7437, answer score: 2

Revisions (0)

No revisions yet.