更新时间:2021-12-31 02:34:36
使用 Xcode 自带的静态分析工具 Product -> Analyze(快捷键 command + shift + B)可以找出代码潜在错误,如内存泄露,未使用函数和变量等。
Analyze 主要分析以下四种问题:
1、user-facing text should use localized string macro
2、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;
}
3、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];
4、Potential leak of an object stored into 'imageRef'
imageRef 对象有内存泄漏
+ (UIImage*)getSubImage:(unsigned long)ulUserHeader {
UIImage * sourceImage = [UIImage imageNamed:@"header.png"];
CGFloat height = sourceImage.size.height;
CGRect rect = CGRectMake(0 + ulUserHeader*height, 0, height, height);
CGImageRef imageRef = CGImageCreateWithImageInRect([sourceImage CGImage], rect);
UIImage* smallImage = [UIImage imageWithCGImage:imageRef];
// CGImageRelease(imageRef);
return smallImage;
}
5、Analyze 逻辑错误监测
所谓静态内存分析,是指在程序没运行的时候,通过 Xcode 自带的静态分析工具 Product -> Analyze(快捷键 command + shift + B)对代码直接进行分析。根据代码的上下文的语法结构,让编译器分析内存情况,检查是否有内存泄露。
缺点:静态内存分析由于是编译器根据代码进行的判断, 做出的判断不一定会准确, 因此如果遇到提示, 应该去结合代码上文检查一下。
内存泄漏提示:Potential leak of an object stored into 'imageRef'
imageRef 对象有内存泄漏
+ (UIImage*)getSubImage:(unsigned long)ulUserHeader {
UIImage * sourceImage = [UIImage imageNamed:@"header.png"];
CGFloat height = sourceImage.size.height;
CGRect rect = CGRectMake(0 + ulUserHeader*height, 0, height, height);
CGImageRef imageRef = CGImageCreateWithImageInRect([sourceImage CGImage], rect);
UIImage* smallImage = [UIImage imageWithCGImage:imageRef];
// CGImageRelease(imageRef);
return smallImage;
}
动态内存分析通过 Xcode 自带的动态分析工具 Xcode -> Product -> Profile(Leaks 工具)动态的对内存进行分析,大多时候只是堆内存的分析。
imageNamed 和 imageWithContentOfFile 方法的比较。
1、imageName 加载图片
a、当 imageview 对象销毁时候,图片对象不会随着一起销毁。
b、加载的图片占据的内存比较大。
c、相同的图片只会加载一份到内存中,如果同时使用,使用的是同一个图片对象。
2、imageWithContentOfFile 加载图片
a、当 imageView 对象销毁的时候,图片对象会随着一起销毁。
b、加载的图片占用的内存比较小。
c、相同的图片对象会多次加载到内存中,如果同时使用图片,使用的是不同的对象。
总结
imageName:如果一些图片在多个界面都会使用,并且图片较小,使用频率高,(图标/小的背景图)。
imageWithContentOfFile:只在一个地方使用,并且图片比较大,使用频率不高,(相册/版本新特性)。