CFRelease中的空指针(由静态分析器讲述)

时间:2012-03-02 08:52:01

标签: ios xcode clang-static-analyzer

我收到了这段代码:

- (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);

}

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

  

调用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]>

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

感谢

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

2 个答案:

答案 0 :(得分:6)

如果你传入NULL,CFRelease将崩溃,因此分析器警告你那里的目标和源可能是NULL。只需添加NULL检查:

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

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

答案 1 :(得分:0)

静态分析器告诉你的是,其中一个参数的可能为null。

例如,如果您在

之后放置一个回报
NSLog(@"***Could not create image destination ***");

警告是否停止?