本文介绍了使用 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类中的leak方法如下:

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中的Memory Leaks,如下图

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