从ALAssetRepresentation获取字节时发生内存泄漏

时间:2015-03-30 12:48:06

标签: memory-leaks

我正在上传相机胶卷中的视频和图片。上传完成后,我得到32字节内存泄漏。仪器指出泄漏在下面

   NSUInteger readStatus = [rep getBytes:buffer fromOffset:_startFromByte length:chunkSize error:NULL];

但我没有看到该行有任何错误。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    __block __typeof__(self) _self = self;

    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) {
        ALAssetRepresentation *rep = [myasset defaultRepresentation];
        NSUInteger chunkSize = CHUNK_SIZE;
        uint8_t *buffer = malloc(chunkSize);
        NSUInteger readStatus = [rep getBytes:buffer fromOffset:_startFromByte length:chunkSize error:NULL];

        if (readStatus == 0) {
            // Free up memory so we don't leak.
            free(buffer);
            dispatch_sync(dispatch_get_main_queue(), ^ {
                [_self cleanupConnectionSuccessful:NO];
            });
            return;
        }

        NSData *bytes = [NSData dataWithBytes:buffer length:readStatus];
        free(buffer);

        dispatch_sync(dispatch_get_main_queue(), ^ {
            [_self prepareRequestAndUploadData:bytes];
        });
    };

    ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *err) {
        NSLog(@"Error: %@", [err localizedDescription]);
    };


    [_assetslibrary assetForURL:_fileUrl resultBlock:resultblock failureBlock:failureBlock];
});

1 个答案:

答案 0 :(得分:0)

只需添加@autoreleasepool块,以便清除任何自动释放的对象。看起来ARC在iOS7之后发生了一些变化

@autoreleasepool {
     NSUInteger readStatus = [rep getBytes:buffer fromOffset:_startFromByte length:chunkSize error:NULL];
}