【问题标题】:Instruments Indicates Memory Leak on Instantiation of NSMutableDictionaryInstruments 指示 NSMutableDictionary 实例化时的内存泄漏
【发布时间】:2014-12-02 07:49:54
【问题描述】:

定位内存泄漏不是我的强项,但在我看来,instruments 在创建 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 时会出现此问题。

【问题讨论】:

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


    【解决方案1】:

    尝试替换

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

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

    【讨论】:

    • 谢谢,当我将它添加到 if 语句的末尾时,这也有效:CFRelease((__bridge CFTypeRef)(gps));
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    • 2011-10-10
    • 2011-06-28
    • 1970-01-01
    相关资源
    最近更新 更多