I have to token the string and get a RGB values to make a UICOlor, below is the code,
NSString* text = @"1.0,1.0,1.0";
NSArray *chunks = [text componentsSeparatedByString:@","];
return [UIColor colorWithRed:([[chunks objectAtIndex:0] floatValue]/256.0)
green:([[chunks objectAtIndex:1] floatValue]/256.0)
blue:([[chunks objectAtIndex:2] floatValue]/256.0)
alpha:1];
It shows me a memory leak at the line returning the UIColor object, hopefully someone will point out the mistake I am doing in the code.
I break the code but it is still showing me a leak at a line where i initialize UIColor object.
The prototype of function is ,
+(UIColor*) GetUIColorFromText:(NSString*)text;
Basically I am getting three weird memory leaks, I don't know whether these three are interrelated or not.
-
1. I am getting a leak when i push my view controller to navigation controller, i.e
MyController *filter = [[MyController alloc] initWithNibName:@"MyController" bundle:nil]; [self.navigationController pushViewController:filter animated:YES]; [filter release] ;this leak get removed when I used animated:NO , i.e
MyController *filter = [[MyController alloc] initWithNibName:@"MyController" bund开发者_开发百科le:nil]; [self.navigationController pushViewController:filter animated:NO]; [filter release] ;2. Leak which i mention in my question.
- Below line of code also show me a memory leak in instrument, I do run the code on IOS 3.12 and as well as 4 but i am getting these three leaks all the time,
[MyBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
See the accepted answer to iphone - UIColor leaking... need to release the object?
It's not actually leaking, it's a false positive in all likelihood.
This code looks perfectly fine to me -- I can't see any stray allocations, so it's possible that the warning is incorrect. It might be worth breaking up the code to localise the warning further, eg:
NSString* text = @"1.0,1.0,1.0";
NSArray *chunks = [text componentsSeparatedByString:@","];
NSString *redStr = [chunks objectAtIndex:0];
float red = [redStr floatValue]/256.0;
NSString *greenStr = [chunks objectAtIndex:1];
float green = [greenStr floatValue]/256.0;
NSString* blueStr = [chunksObjectAtIndex:2];
float blue = [blueStr floatValue]/256.0;
UIColor* rgb = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
return rgb;
This should at least identify exactly where the reported problem is. However, I'm almost certain there is no actual leak in this code.
A couple of other thoughts:
Is this the full extent of the code? Presumably you're not using a hardcoded @"1.0,1.0,1.0"
in the long run, so is there something else in the vicinity that may be causing the problem?
What's the method name? I've read hereabouts that the static analyser takes into account the NARC naming convention when trying to work out what the semantics are meant to be. I've no idea how true that is, nor how it might cause your problem here, but there's a very remote chance that this might be contributing to an erroneous leak warning.
EDIT: based on your additional info, I'm at a loss. All three of those snippets look OK to me. What are you using to detect these leaks -- static analysis or runtime? If the latter, are you running on the simulator or the device? Is any other information provided?
精彩评论