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!
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
allocon an object increments the reference count – to decrement the reference count areleaseought to be called.For info on ARC see: How does the new automatic reference counting mechanism work?