-2

Possible Duplicate:
Local variable assign versus direct assign; properties and memory
Which way is the correct way to allocate memory with a property?

I wanted to know the difference between this two code examples.

1:

NSString *someString = @"Blabla";


{...Some code...}


imageView.title = [[NSString alloc] initWithString:someString];

2:

NSString *someString = @"Blabla";


{...Some code...}


NSString *str = [[NSString alloc] initWithString:someString];
imageView.title = str;
[str release];

For some reason Xcode Analyzer warns me that option #1 might cause to memory leak- so when I change my code to option #2 the analyzer doesn't warn me.

Does anyone know Whats the reason for that?

Thank you very much!

Community
  • 1
  • 1
  • possible duplicate of [Local variable assign versus direct assign; properties and memory](http://stackoverflow.com/questions/3066848/), http://stackoverflow.com/questions/7395253/, http://stackoverflow.com/questions/8605078/, http://stackoverflow.com/questions/7017046/, http://stackoverflow.com/questions/5447063/, http://stackoverflow.com/questions/2818254/, and so many more. – jscs May 11 '12 at 17:12
  • In option 2 , you have releasesd, whereas in 1 you have not. so the warning. – zahreelay May 11 '12 at 18:10

3 Answers3

1

Assuming you are not using ARC, the

[str release];

line is the key here. Add that line to the end of the first example snippet and see if you get the same warning. Explicitly calling alloc on an object increments the reference count - to decrement the reference count a release ought to be called.

For info on ARC see: How does the new automatic reference counting mechanism work?

Community
  • 1
  • 1
Nate Flink
  • 3,934
  • 2
  • 30
  • 18
0

In the first example, you allocated a new NSString, passed it on, and didn't release it. You're responsible to releasing that string, which has a +1 retain count when you first allocate it.

(Ignoring the fact that your simple example probably won't cause an actual leak. But that's not the point, it's still not the correct way to manage that memory here)

Firoze Lafeer
  • 17,133
  • 4
  • 54
  • 48
0

The problem in your first example:

NSString *someString = @"Blabla";
{...Some code...}
imageView.title = [[NSString alloc] initWithString:someString];

is that you are allocating through alloc/init a string and assigning it to imageView.title which is a retain property. Indeed, alloc will give you an object with a retain count of 1; assigning it to the retain property will also increment its retain count (2). Now, when the imageView is finally deallocated, its dealloc method will release the title property (thus decrementing its retain count by 1), but the object will not be deallocated because you have no chance to call release once again to balance your own alloc.

The important thing to notice here is that you are assigning to a retain property; were title not a retain property, your code would be fine.

This is how you can fix your first example:

imageView.title = [[[NSString alloc] initWithString:someString] autorelease];

or

imageView.title = [NSString stringWithString:someString];

which relies on the use of a commodity constructor which gives you an autoreleased object by convention.

(it is also correct the way you do it in your second example, though slightly more verbose).

Firoze Lafeer
  • 17,133
  • 4
  • 54
  • 48
sergio
  • 68,819
  • 11
  • 102
  • 123