简体   繁体   中英

Instruments Indicates Memory Leak on Instantiation of NSMutableDictionary

Locating memory leaks is not my strong suit, but it appears to me that instruments is indicating a memory leak upon the creation of, and the setting of values for, an NSMutableDictionary. (I am using ARC). This makes no sense to me as of yet, hopefully someone might know what is actually happening.

I have a delegate method which is called on the completion of taking a photo with the device's camera: (I have added a comment for the lines which are raising an issue).

- (void)finishImageCaptureForBuffer:(CMSampleBufferRef)imageDataSampleBuffer withError:(NSError *)error shouldSave:(BOOL)save
{ //do some stuff...

if (save) {
    CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault,
                                                                self.imageDataSampleBuffer,
                                                                kCMAttachmentMode_ShouldPropagate);
    CFMutableDictionaryRef mutableAttachments = CFDictionaryCreateMutableCopy(NULL, 0, attachments);

    //MEMORY LEAK!!!!!//
    NSMutableDictionary *gps = [NSMutableDictionary dictionary];

    CLLocation *location = [manager location];
    [gps setObject:@"" forKey:(NSString *)kCGImagePropertyGPSVersion];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm:ss.SSSSSS"];
    [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

    //MEMORY LEAK!!!!!//
    [gps setObject:ObjectOrNull([formatter stringFromDate:location.timestamp])
            forKey:(NSString *)kCGImagePropertyGPSTimeStamp];

    [gps setObject:(location.coordinate.latitude < 0) ? @"S" : @"N"
            forKey:(NSString *)kCGImagePropertyGPSLatitudeRef];

     //MEMORY LEAK!!!!!
    [gps setObject:ObjectOrNull([NSNumber numberWithDouble:fabs(location.coordinate.latitude)])
            forKey:(NSString *)kCGImagePropertyGPSLatitude]; 

    [gps setObject: (location.coordinate.longitude < 0) ? @"W" : @"E"
            forKey:(NSString *)kCGImagePropertyGPSLongitudeRef];

    //MEMORY LEAK!!!
    [gps setObject:ObjectOrNull([NSNumber numberWithDouble:fabs(location.coordinate.longitude)])
            forKey:(NSString *)kCGImagePropertyGPSLongitude];

    CFDictionarySetValue(mutableAttachments, kCGImagePropertyGPSDictionary, CFBridgingRetain(gps));

    NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:self.imageDataSampleBuffer];
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    [library writeImageDataToSavedPhotosAlbum:imageData
                                     metadata:(__bridge id)mutableAttachments
                              completionBlock:completionBlock];

    if ([[self delegate] respondsToSelector:@selector(captureManagerStillImageCaptured:)]) {
        [[self delegate] captureManagerStillImageCaptured:self];
    }

    CFRelease(attachments);
    CFRelease(mutableAttachments);
    CFRelease(self.imageDataSampleBuffer);

} else { ...

Where objectOrNull checks to see if the value we are inserting exists, and if it does not, [NSNull null] is added to the dictionary.

As an aside, this issue appears when running iOS7 but not iOS8.

Try replacing

CFDictionarySetValue(mutableAttachments, kCGImagePropertyGPSDictionary, CFBridgingRetain(gps));

with

CFDictionarySetValue(mutableAttachments, kCGImagePropertyGPSDictionary, (__bridge CFDictionaryRef)gps);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM