美文网首页
如何管理内存:

如何管理内存:

作者: Emma_虫儿 | 来源:发表于2019-02-25 16:55 被阅读0次

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.

相关文章

  • OS-Memory Management

    进程管理:一组进程如何共享CPU内存管理:一组进程如何共享内存 stall ,cache memory和regis...

  • OC基础(六)——内存管理

    内存管理概述 内存管理内存的作用:存储数据. 如何将数据存储到内存之中.声明1个变量.然后将数据存储进去. 当数据...

  • 【问答】补充

    Java JVM如何管理内存的? Java中内存管理是JVM自动进行的,创建对象或者变量时JVM会自动分配内存,当...

  • Java是如何管理内存

    为了判断Java中是否有内存泄露,我们首先必须了解Java是如何管理内存的。Java的内存管理就是对象的分配和释放...

  • 2. 托管内存

    对于Unity内存管理而言,需要理解托管堆。对于如何分析托管内存和如何优化内存,可以参见Unity优化中的理解托管...

  • redis内存优化的探索和实践

    1,redis如何管理内存结构 redis内存模型: 【used_memory】:Redis内存占用中最主要的部分...

  • iOS | 内存管理

    如何使用内存以及如何分配内存对于设备和用户体验至关重要。OC语言作为C语言的超集,对于内存以及内存管理方面非常重要...

  • 如何管理内存:

    1. Returning 'self' while it is not set to the result of ...

  • Java内存泄漏

    Java中的内存管理 要了解Java中的内存泄漏,首先就得知道Java中的内存是如何管理的。 在Java程序中,我...

  • 说说Java内存泄漏

    Java中的内存管理 要了解Java中的内存泄漏,首先就得知道Java中的内存是如何管理的。 在Java程序中,我...

网友评论

      本文标题:如何管理内存:

      本文链接:https://www.haomeiwen.com/subject/krsyyqtx.html