boscarol
boscarol

Reputation: 1941

Potential memory leak

I work on a project on iPhone iOS with Xcode 4.

With Xcode > Product >Analyze I get 35 issues, all of this type:

 myTextField.text = [[NSString alloc] initWithFormat:@"0.2f", abc];

and the problem is "Potential leak of an object allocated at ..."

What is the offending object and how can I release it?

Thanks

Upvotes: 0

Views: 595

Answers (3)

csano
csano

Reputation: 13676

You're leaking the string that you're assigning to myTextField.text. When this assignment happens, a copy is being made (look at the property definition in the documentation). In most cases, when values are immutable, as is the case with NSStrings, a copy will give you an instance that points to the same location as the object that is being copied, with the retain count incremented by 1.

In the case of your code:

myTextField.text = [[NSString alloc] initWithFormat:@"0.2f", abc];

The retain count of the string that you've allocated is 2.

You will either need to (1) release, (or autorelease) the string, or (2) use one of the NSString convenience methods, e.g. stringWithFormat: to create the string. This will give you an autoreleased string so you won't have to worry about explicitly releasing it.

(1)

NSString *str = [[NSString alloc] initWithFormat:@"0.2f", abc];
myTextField.text = str;
[str release]

or

myTextField.text = [[[NSString alloc] initWithFormat:@"0.2f", abc] autorelease];

(2)

myTextField.text = [NSString stringWithFormat:@"0.2f", abc]; // autoreleased

Upvotes: 5

Oscar Gomez
Oscar Gomez

Reputation: 18488

The thing is that UiTextFields's text property is declared as:

@property(nonatomic, copy) NSString *text

Therefore in this line:

 myTextField.text = [[NSString alloc] initWithFormat:@"0.2f", abc];

A new NSString is created with a retain count of 1, and then myTextField.text copies this object and increased its retain count by 1 or does it??, lets see what is happening:

  1. A NSString object created with alloc initWithFormat with a retain count of 1
  2. A NSString object with is a copy of the previous String, but because NStrings are immutable in this case, copy returns the same object!, therefore the NSString actually has a retain count of 2.

Upvotes: 2

Vladimir
Vladimir

Reputation: 170829

You are responsible for releasing string object you create here - as you use alloc/init for that.

The most convenient way here to set a string is to use class method +stringWithFormat that returns autoreleased string - so system will release that string object for you later:

myTextField.text = [NSString stringWithFormat:@"0.2f", abc];

Or you can write autorelease explicitly if you want:

myTextField.text = [[[NSString alloc] initWithFormat:@"0.2f", abc] autorelease];

If you don't want to use autorelease you can use temporary variable to create new string and release it after it was set for text field:

NSString *tempString = [[NSString alloc] initWithFormat:@"0.2f", abc];
myTextField.text = tempString;
[tempString release];

Upvotes: 4

Related Questions