1. Returning 'self' while it is not set to the result of '[(super or self) init...]'
有可能造成這個(gè)結(jié)果的原因是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)重復(fù)開辟了空間母怜。具體表現(xiàn)在:
<code>NSArray* arrImage? =[ [NSArray alloc]init];?
arrImage = [NSArray arrayWithObjects:@"1", @"2", nil];//錯(cuò)誤</code>
//此處應(yīng)該只是聲明NSArray* arrImage祭刚;即可因?yàn)閍rrayWithObjects是“便捷構(gòu)造”懦趋。 它會(huì)做什么:return [[[NSArray alloc] initWithObjects:@"hai",@"how",@"are",@"you",nil] autorelease]
2) NSString*weiVo? = [NSStringalloc]init;+ stringWithFormat引起。
(1)抽莱、initWithFormat是實(shí)例辦法
只能經(jīng)由過(guò)程 NSString* str = [[NSString alloc] initWithFormat:@"%@",@"Hello World"] 調(diào)用衅斩,然則必須手動(dòng)release來(lái)開釋內(nèi)存資料
(2)勘伺、stringWithFormat是類辦法
可以直接用 NSString* str = [NSString stringWithFormat:@"%@",@"Hello World"] 調(diào)用昌腰,內(nèi)存經(jīng)管上是autorelease的开伏,不消手動(dòng)顯式release
并且提出了一個(gè)常見錯(cuò)誤:label.text = [[NSString alloc] initWithFormat:@"%@",@"abc"];最后在dealloc中將label給release掉;然則仍然會(huì)產(chǎn)生內(nèi)存泄漏遭商!
原因在于:用label.text = ...時(shí)硅则,實(shí)際是隱式調(diào)用的label的setText辦法,這會(huì)retain label內(nèi)部的字符串變量text(哪怕這個(gè)字符串的內(nèi)容跟傳進(jìn)來(lái)的字符串內(nèi)容雷同株婴,但體系仍然當(dāng)成二個(gè)不合的字符串對(duì)象),所以最后release label時(shí)暑认,實(shí)際上只開釋了label內(nèi)部的text字符串困介,然則最初用initWithFormat生成的字符串并未開釋,終極造成了泄漏蘸际。
解決辦法有二個(gè):
(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
造成這個(gè)的可能原因是字典中的對(duì)象有的沒有初始化。
4.Property of mutable type 'NSMutableDictionary' has 'copy' attribute; an immutable object will be stored instead
這個(gè)檢測(cè)的是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? 取圖片某一點(diǎn)的顏色
?*
?*? @param point 某一點(diǎn)
?*
?*? @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.