In my app i get a memory leak in
first
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
result = [[NSString alloc] initWithBytes:[webData mutableBytes]
length:[webData length]
encoding:NSUTF8StringEncoding];
[webData release];
}
second
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString * result = [[NSString alloc] initWithBytes:[webData mutableBytes]
length:[webData length]
encoding:NSUTF8StringEncoding];
[webData release];
}
in my first process I didn’t get a memory leak(Declare a string object globally and I didn’t release it)
In my second process i get a memory leak in string object.
-
Value stored to ‘result’ during its initialization is never read
-
Method returns an Objective-C object with a +1 retain count (owning reference)
-
Object allocated on line 124 and stored into ‘result’ is no longer referenced after this point and has a retain count of +1 (object leaked)
what is the difference?
In first case analyzer expects you to release your initted
resultstring in, say,- (void)deallocmethod of the class. If you don’t – you’ll get a leak as well (you’ll see the leak if you run your app with via Instruments app with Leaks instrument added.In second case you’re creating a local variable in method scope where it should be released as well since you won’t have a reference of it in any other method (i.e. if you try access
resultvariable in other method you’ll get unknown identifier error).