CFRelease中的空指针(由static anylizer讲述)

Poc*_*chi 1 xcode clang-static-analyzer ios

我得到了这段代码:

- (void)saveImageWithData:(NSData*)jpeg andDictionary:(NSDictionary*)dicRef andName:(NSString*)name
{
    self.capturedImageName = name;
    self.dict = dicRef;

    NSLog(@"%@",dicRef);

    CGImageSourceRef  source ;
    source = CGImageSourceCreateWithData((__bridge CFDataRef)jpeg, NULL);

    CFStringRef UTI = CGImageSourceGetType(source); //this is the type of image (e.g., public.jpeg)

    NSMutableData *dest_data = [NSMutableData data];


    CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)dest_data,UTI,1,NULL);

    if(!destination) {
        NSLog(@"***Could not create image destination ***");
    }

    //add the image contained in the image source to the destination, overwriting the old metadata with our modified metadata
    CGImageDestinationAddImageFromSource(destination,source,0, (__bridge CFDictionaryRef) dicRef);

    //tell the destination to write the image data and metadata into our data object.
    //It will return false if something goes wrong
    BOOL success = NO;
    success = CGImageDestinationFinalize(destination);

    if(!success) {
        NSLog(@"***Could not create data from image destination ***");
    }

    //now we have the data ready to go, so do whatever you want with it
    //here we just write it to disk at the same path we were passed

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"ARPictures"];

    NSError *error;
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder

    NSString *fullPath = [dataPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", name]]; //add our image to the path

    [dest_data writeToFile:fullPath atomically:YES];

    self.img = [[UIImage alloc] initWithData:dest_data]; 
    self.capturedImageData = [[NSData alloc] initWithData:dest_data];

    //cleanup

    CFRelease(destination);
    CFRelease(source);

}
Run Code Online (Sandbox Code Playgroud)

但是,当我运行静态分析仪时,它告诉我:

调用CFRelease时出现空指针参数

但根据我的逻辑,我应该发布由CGImageDestinationCreateWithData创建的CGImageSourceRef.

另外我认为释放它可能是一个错误,因为我只是桥接它,这意味着弧仍然控制该对象,因为它最初是一个NSMutableData对象.

更糟糕的是,我读到CFRelease(Null)并不好.

我非常困惑,任何帮助将不胜感激.

编辑:

好吧,在将它们发送到CFRelease之前,我已经记录了指针,它们就在那里!

2012-03-03 11:35:34.709 programtest[4821:707] <CGImageDestination 0x331790 [0x3f92d630]>

2012-03-03 11:35:34.723 programtest[4821:707] <CGImageSource 0x33ee80 [0x3f92d630]>
Run Code Online (Sandbox Code Playgroud)

回到最初的问题,静态分析器为什么告诉我发送空指针参数?以及如何修复/静音?

谢谢

PD:我可能没有发送空指针而是指向null的指针吗?

dav*_*den 6

如果传入NULL,CFRelease将崩溃,因此分析器会警告您目标和源可能为NULL.只需添加NULL检查:

if ( destination != NULL )
    CFRelease(destination);

if ( source != NULL )
    CFRelease(source);
Run Code Online (Sandbox Code Playgroud)