1. Returning 'self' while it is not set to the result of '[(super or self) init...]'
有可能造成这个结果的原因是if(self= [superinitWithFrame:frame])写的是“==”
<code>- (instancetype)initWithFrame:(CGRect)frame
{
if(self= [superinitWithFrame:frame]) {
[self createMapView];
}
return self;
}</code>
2. Value stored to 'arrImage' during its initialization is never read
可能原因:1)重复开辟了空间。具体表现在:
<code>NSArray* arrImage =[ [NSArray alloc]init];
arrImage = [NSArray arrayWithObjects:@"1", @"2", nil];//错误</code>
//此处应该只是声明NSArray* arrImage;即可因为arrayWithObjects是“便捷构造”。 它会做什么:return [[[NSArray alloc] initWithObjects:@"hai",@"how",@"are",@"you",nil] autorelease]
2) NSString*weiVo = [NSStringalloc]init;+ stringWithFormat引起。
(1)、initWithFormat是实例办法
只能经由过程 NSString* str = [[NSString alloc] initWithFormat:@"%@",@"Hello World"] 调用,然则必须手动release来开释内存资料
(2)、stringWithFormat是类办法
可以直接用 NSString* str = [NSString stringWithFormat:@"%@",@"Hello World"] 调用,内存经管上是autorelease的,不消手动显式release
并且提出了一个常见错误:label.text = [[NSString alloc] initWithFormat:@"%@",@"abc"];最后在dealloc中将label给release掉;然则仍然会产生内存泄漏!
原因在于:用label.text = ...时,实际是隐式调用的label的setText办法,这会retain label内部的字符串变量text(哪怕这个字符串的内容跟传进来的字符串内容雷同,但体系仍然当成二个不合的字符串对象),所以最后release label时,实际上只开释了label内部的text字符串,然则最初用initWithFormat生成的字符串并未开释,终极造成了泄漏。
解决办法有二个:
(1)、NSString * str = [[NSString alloc] initWithFormat:@"%@",@"abc"];
label.text = str;[str release];
最后在dealloc中再[label release]
(2)、label.text = [NSString stringWithFormat:@"%@",@"abc"];
然后剩下的工作交给NSAutoreleasePool
3.Dictionary value cannot be nil
造成这个的可能原因是字典中的对象有的没有初始化。
4.Property of mutable type 'NSMutableDictionary' has 'copy' attribute; an immutable object will be stored instead
这个检测的是NSMutable*的属性不能添加copy修饰。
5.Potential leak of an object stored into 'colorSpace'
代码分析:1. Assuming 'rawData' is non-null
2. Call to function 'CGColorSpaceCreateDeviceRGB' returns a Core Foundation object of type CGColorSpaceRef _Nullable with a +1 retain count
3. Assuming 'context' is null
4. Object leaked: object allocated and stored into 'colorSpace' is not referenced later in this execution path and has a retain count of +1
<code>/**
* @brief 取图片某一点的颜色
*
* @param point 某一点
*
* @return 颜色
*/
- (UIColor*)colorAtPoint:(CGPoint)point
{
if(point.x<0|| point.y<0)
returnnil;
CGImageRefimageRef =self.CGImage;
NSUIntegerwidth =CGImageGetWidth(imageRef);
NSUIntegerheight =CGImageGetHeight(imageRef);
if(point.x>= width || point.y>= height)returnnil;
unsignedchar*rawData =malloc(height * width *4);
if(!rawData)returnnil;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSUIntegerbytesPerPixel =4;
NSUIntegerbytesPerRow = bytesPerPixel * width;
NSUIntegerbitsPerComponent =8;
CGContextRefcontext =CGBitmapContextCreate(rawData,
width,
height,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast
|kCGBitmapByteOrder32Big);
if(!context) {
CGColorSpaceRelease(colorSpace);//这两句就是解决办法
CGContextRelease(context);//这一句也是
free(rawData);
returnnil;
}
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context,CGRectMake(0,0, width, height), imageRef);
CGContextRelease(context);
intbyteIndex = (bytesPerRow * point.y) + point.x* bytesPerPixel;
CGFloatred = (rawData[byteIndex] *1.0) /255.0;
CGFloatgreen = (rawData[byteIndex +1] *1.0) /255.0;
CGFloatblue = (rawData[byteIndex +2] *1.0) /255.0;
CGFloatalpha = (rawData[byteIndex +3] *1.0) /255.0;
UIColor*result =nil;
result = [UIColorcolorWithRed:redgreen:greenblue:bluealpha:alpha];
free(rawData);
returnresult;
}
</code>
6.