最近在做内存泄露检测,记录一下遇到的问题,欢迎大家补充 🙂
1.instance variable used while 'self' is not set to the result of '[(super or self) init...]
//提示错误
-(instancetype)initWithType:(FTFFavorateType)type
{
if (self == [super init]) {
_type = type;
}
return self;
}
//修改为如下
- (instancetype)initWithType:(FTFFavorateType)type
{
if (self = [super init]) {
_type = type;
}
return self;
}
2.Value stored to ‘durationValue’ during its initialization is never read(在初始化过程中存储的“持续时间值”的值永远不会被读取)
// 此段代码提示错误
NSMutableArray *datesArray = [[NSMutableArray alloc] init];
datesArray = [_onDemandDictionary objectForKey:key];
- 这是因为[NSMutableArray alloc] init]初始化分配了内存,而判断语句里面[_onDemandDictionary objectForKey:key]方法也相当于初始化分配了内存,就是把初始化的一个新的可变数组赋值给之前已经初始化过的可变数组,看似没什么大问题,其实存在一个数据源却申请了两块内存的问题,已经造成了内存泄露。
//修改为如下
NSMutableArray *datesArray = nil;
datesArray = [_onDemandDictionary objectForKey:key];
3.User-facing text should use localized string macro
- 工程配置里 Build Settings 中的 Missing Localizability 设置为 NO
4.nil returned from a method that is expected to return a non-null value
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
return cell;
}
//修改为
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
arort();
}
5.Potential leak of an object stored into 'xxx'
- 由于iOS中CoreFoundation框架需要自己释放内存,所以ARC的自动释放内存就不管用了,需要我们自己释放,需要使用CFRelease(<#CFTypeRef cf#>)
6.the left operand of ** is a garbage value
CGPoint point;
float height = point.y + 5.0;
//修改为
CGPoint point = CGPointZero;
float height = point.y + 5.0;
7.value stored to is never read
8.Property of mutable type 'NSMutableString' has 'copy' attribute; an immutable object will be stored instead
@property (nonatomic,copy) NSMutableString *str;
//修改为
@property (nonatomic,strong) NSMutableString *str;