1

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 method cause the memory leak mainly. Sometimes CGDataProviderCreateWithData also cause memory leak. But i don't know why.

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;
    }
}

And I release mBitmap in the dealloc.

Thanks in advance.

chancyWu
  • 14,073
  • 11
  • 62
  • 81

2 Answers2

1

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

Screenshot1

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

Screenshot2

Vimal Venugopalan
  • 4,091
  • 3
  • 16
  • 25
  • if you can see the leaks.. can you mention the leaks in your question – Vimal Venugopalan Aug 31 '12 at 09:02
  • I go through Leaks->Extended Detail then double click the function name, it locate one line in the method, which mainly caused by CGImageCreateWithJPEGDataProvider in my method. – chancyWu Aug 31 '12 at 09:05
  • can u mention what the memory leak shown by xcode (e.g. in my case **Value stored to 'copyView' during its initialization is never used**) – Vimal Venugopalan Sep 01 '12 at 09:38
  • I use analyzer,it says the methods are all right. When i use leaks of instruments, sometimes, it shows me memory leaks of the method. you know, the leaks only locate the line of leak, no others. – chancyWu Sep 03 '12 at 03:58
0

You should free the memory you allocated for your buffer.

free(buffer);
orkoden
  • 18,946
  • 4
  • 59
  • 50