仪器表示NSMutableDictionary实例化时的内存泄漏

时间:2014-10-07 19:20:16

标签: ios memory-management memory-leaks instruments nsmutabledictionary

查找内存泄漏不是我的强项,但在我看来,仪器在NSMutableDictionary的创建和设置值时指示内存泄漏。 (我正在使用ARC)。这对我来说没有任何意义,希望有人可能知道实际发生了什么。

我有一个委托方法,在完成使用设备的相机拍照时调用:(我已经为引发问题的行添加了注释)。

- (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 { ...

其中objectOrNull检查我们插入的值是否存在,如果不存在,则[NSNull null]被添加到字典中。

另外,运行iOS7但不是iOS8时会出现此问题。

1 个答案:

答案 0 :(得分:0)

尝试替换

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

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