1

找到內存泄漏並不是我的強項,但在我看來,儀器正在爲NSMutableDictionary創建和設置值時指示內存泄漏。 (我正在使用ARC)。這對我來說沒有任何意義,希望有人可能知道實際發生的事情。儀器指示內存泄漏對NSMutableDictionary的實例化

我有一個委託方法,它是在完成使用設備相機拍攝照片時調用的:(我爲引發問題的行添加了註釋)。

- (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空]被添加到字典中。

另一方面,運行iOS7而不是iOS8時出現此問題。

回答

0

嘗試用

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

由於更換

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

,這也是工作時,我把它添加到,如果statment結束:CFRelease((__橋CFTypeRef)(GPS)); – jac300 2014-10-07 20:26:45