用CGImageCreateWithJPEGDataProvid

用CGImageCreateWithJPEGDataProvid

本文介绍了使用CGImageCreateWithJPEGDataProvider时内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Instruments(Leaks)来检测我的应用程序的内存泄漏。它有时会说我的一种方法中存在内存泄漏。我不确定是否存在泄漏以及如何解决它。

I use Instruments(Leaks) to detect memory leaks of my app. It sometimes says there is a memory leak in one of my methods. I am not sure whether there is a leak and how to solve it.

CGImageCreateWithJPEGDataProvider方法主要导致内存泄漏。有时CGDataProviderCreateWithData也会导致内存泄漏。但我不知道为什么。

CGImageCreateWithJPEGDataProvider method cause the memory leak mainly. Sometimes CGDataProviderCreateWithData also cause memory leak. But i don't know why.

A类的泄漏方法如下:

The leak method in Class A is as follows:

- (ResultHolder *)decodeData:(UInt8 *) data withOffset:(int) offset {

    const int length = [IOUtilities byteArrayToIntWithData:data Offset:offset+18];
    UInt8 *buffer = malloc(length*sizeof(UInt8));
    memcpy(buffer, data+offset+22, length);

    // sometimes memory leak in this line
    CGDataProviderRef jpgDataProvider = CGDataProviderCreateWithData(NULL, buffer, length, freeData);

    // mainly memory leak in this line.
    CGImageRef image = CGImageCreateWithJPEGDataProvider(jpgDataProvider, NULL, true, kCGRenderingIntentSaturation);


    CGDataProviderRelease(jpgDataProvider);
    ResultHolder *result = [[ResultHolder alloc] initWithCGImage:image];//sometimes memory leaks in this line.
    CGImageRelease(image);
    return result;
}

还有一些其他相关方法:

Also some other related methods:

// class A free buffer callback.
void freeData(void *info, const void *data, size_t size) {
    free((void *)data);
}
// class ReslutHolder init and dealloc.
- (id)initWithCGImage:(CGImageRef)image {
    if (self = [super init]) {
        CGImageRetain(image);

        //sometimes memory leak in this line.
        mBitmap = CGImageCreateCopy(image);

        mWidth = CGImageGetWidth(image);
        mHeight = CGImageGetHeight(image);
        CGImageRelease(image);
    }
    return self;
}

- (void)dealloc {
    if (mBitmap != NULL) {
        CGImageRelease(mBitmap);
        mBitmap = NULL;
    }
}

我在dealloc中发布了mBitmap。

And I release mBitmap in the dealloc.

提前致谢。

推荐答案

您可以分析Xcode中的内存泄漏使用如下所示的分析器

You can analyze the Memory Leaks in the Xcode using the analyzer as shown below

提及下面显示的内存泄漏将有助于进一步解决您的问题

Mentioning the memory leaks shown as below would help in further solving your problem

这篇关于使用CGImageCreateWithJPEGDataProvider时内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 06:06