如何管理内存:

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.

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,161评论 1 32
  • //设置尺寸为屏幕尺寸的时候self.window = [[UIWindow alloc] initWithFra...
    LuckTime阅读 849评论 0 0
  • 哦吼吼,又研究了几天,把FMDB这个封装好的数据库搞定了,写了个简单的例子,基于FMDB的添删改查操作,界面很一般...
    lichengjin阅读 584评论 0 0
  • 1、禁止手机睡眠 [UIApplicationsharedApplication].idleTimerDisabl...
    小热狗阅读 937评论 0 2
  • 读了一篇不错的文章,逻辑清晰,文笔流畅,启发性很强,大赞。于是迫不及待地将其发给了身边的朋友分享,并附上了感言,意...
    木韦吉土阅读 745评论 0 0