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.

8
  • Are you using ARC? If not please try to use ARC Commented Aug 31, 2012 at 8:01
  • 3
    This are Quartz 2D objects... How does ARC help ? Commented Aug 31, 2012 at 8:08
  • I am using ARC. But as @mientus said,these methods are Quartz 2D objects. Commented Aug 31, 2012 at 8:13
  • Have you tried to run the static analyzer ? Commented Aug 31, 2012 at 8:19
  • @Geoffroy yes,i have tried the analyzer.its right for these methods. Commented Aug 31, 2012 at 8:21

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

if you can see the leaks.. can you mention the leaks in your question
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.
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)
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.
0

You should free the memory you allocated for your buffer.

free(buffer);

1 Comment

I free the buffer in freeData callback method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.