首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CGBitmapContextCreate中的泄漏

CGBitmapContextCreate中的泄漏
EN

Stack Overflow用户
提问于 2010-10-28 03:50:04
回答 1查看 1.8K关注 0票数 1

为了找出代码中的漏洞,我删除了所有无关紧要的东西,只留下了以下代码:

代码语言:javascript
复制
static int last_memory = 0;

void report_memory(NSString *name) {
    struct task_basic_info info;
    mach_msg_type_number_t size = sizeof(info);
    kern_return_t kerr = task_info(mach_task_self(),
                                   TASK_BASIC_INFO,
                                   (task_info_t)&info,
                                   &size);
    if( kerr == KERN_SUCCESS ) {
        int change = (int)info.resident_size - last_memory;
        NSLog(@"%@ >>> Memory in use: %u (change: %d Kb)", name, info.resident_size, change/1024 );
        last_memory = info.resident_size;
    } else {
        NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
    }
}

- (CGContextRef) createBitmapContextOfSize:(CGSize) size {
    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    void *          bitmapData;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;

    bitmapBytesPerRow   = (size.width * 4);
    bitmapByteCount     = (bitmapBytesPerRow * size.height);

    colorSpace = CGColorSpaceCreateDeviceRGB();
    bitmapData = malloc( bitmapByteCount );
    if (bitmapData == NULL) {
        NSLog(@"Memory not allocated!");
        CGColorSpaceRelease( colorSpace );
        return NULL;
    }
    context = CGBitmapContextCreate (bitmapData,
                                     size.width,
                                     size.height,
                                     8,      // bits per component
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     kCGImageAlphaPremultipliedLast);
    CGContextSetAllowsAntialiasing (context,NO);
    if (context== NULL) {
        free (bitmapData);
        CGColorSpaceRelease( colorSpace );
        NSLog (@"Context not created!");
        return NULL;
    }
    CGColorSpaceRelease( colorSpace );
    return context;
}

- (IBAction) clickButton:(UIButton *)sender {
    report_memory(@"begin");
    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test"];
    UIImage *image = [UIImage imageWithContentsOfFile:path];

    report_memory(@"image_created");

    CGContextRef myBitmapContext = [self createBitmapContextOfSize:CGSizeMake(256, 256)];
    if (!myBitmapContext){
        return;
    }
    report_memory(@"bitmap_context_created");
    CGContextDrawImage(myBitmapContext, CGRectMake(0, 0, 256, 256), image.CGImage);
    report_memory(@"draw_image");
    CGImageRef ref = CGBitmapContextCreateImage(myBitmapContext);
    report_memory(@"image_context_created");
    CGContextRelease(myBitmapContext);
    report_memory(@"bitmap_context_released");
    UIImage *result = [UIImage imageWithCGImage:ref];
    report_memory(@"image_created");
    CGImageRelease(ref);
    report_memory(@"image_context_released");   
    imageView.image = result;
    report_memory(@"image_assigned");   
}

("test“是256x256的JPEG图像)。单击几下后,控制台显示以下内容:

代码语言:javascript
复制
begin >>> Memory in use: 18866176 (change: -12 Kb)
image_created >>> Memory in use: 18870272 (change: 4 Kb)
bitmap_context_created >>> Memory in use: 18870272 (change: 0 Kb)
draw_image >>> Memory in use: 19140608 (change: 264 Kb)
image_context_created >>> Memory in use: 19140608 (change: 0 Kb)
bitmap_context_released >>> Memory in use: 19140608 (change: 0 Kb)
image_created >>> Memory in use: 19140608 (change: 0 Kb)
image_context_released >>> Memory in use: 19140608 (change: 0 Kb)
image_assigned >>> Memory in use: 19140608 (change: 0 Kb)

每次点击净损失256Kb (相当于256x256x4)。在分配性能工具中也可以看到同样的趋势。所有指向CGBitmapContext的信息都没有发布。我只是不知道我做错了什么.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-10-28 04:24:14

通过提供NULL作为第一个参数(bitmapData),让CGBitmapContextCreate自己分配内存。这让你从管理你的内存分配中解脱出来--你现在还没有释放内存。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4037098

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档