Analyze静态分析内存泄露修复
2020-06-29 本文已影响0人
天天想念
1.Value stored to 'tempA' during its initialization is never read
容易出现这个问题的情况:一个数据源却申请了两块内存。导致另外一个内存没用了。如:
例子1:想把两个可变数组分情况赋值
//NSMutableArray * tempA = [NSMutableArray arrayWithCapacity:0]; //错误
NSMutableArray * tempA; //正确:只需做一个可变数组tempMutArr的声明,不需要给它分配实际内存
if (self.mSwitch.isOn) {
tempA = self.array1;
}else{
tempA = self.array2;
}
例子2:取值
//NSMutableArray *datesArray = [[NSMutableArray alloc]init];//错误
NSMutableArray *datesArray = nil; //正确
datesArray = [_onDemandDictionary objectForKey:key];
上述标明错误的语句的的错误原因是:那样做会导致整段代码下来出现了一个数据源却申请了两块内存的情况。从而导致静态检测内存泄露的时候,有有内存泄漏的提示 Value stored to 'tempMutArr' during its initialization is never read。
2.Converting a pointer value of type 'NSNumber *' to a primitive boolean value; instead, either compare the pointer to nil or call -boolValue
将类型为“NSNumber *”的指针值转换为原始布尔值; 相反,要么将指针与nil进行比较,要么调用-boolValue
修改前
NSNumber *delayTimeUnclampedProp = gifProperties[(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
if (delayTimeUnclampedProp) {
frameDuration = [delayTimeUnclampedProp floatValue];
}
修改后
NSNumber *delayTimeUnclampedProp = gifProperties[(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
if (delayTimeUnclampedProp !=nil) {
frameDuration = [delayTimeUnclampedProp floatValue];
}
3.Property of mutable type 'NSMutableString' has 'copy' attribute; an immutable object will be stored instead
NSMutableString类型是可变字符串对象,使用strong修饰
@property (nonatomic,strong) NSMutableString *pwStr;
4.nil passed to a callee that requires a non-null 1st parameter
nil传递给需要非null第一个参数的被调用者
只需要添加一个非空判断,保证传入的参数不为空即可