Search code examples
iosxcodeclang-static-analyzer

Null pointer in CFRelease (told by static anylizer)


I got this code:

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

}

But when I run the static analyzer it tells me that:

Null pointer argument in call to CFRelease

But according to my logic I should be releasing the CGImageSourceRef which is created by a CGImageDestinationCreateWithData.

Also I am thinking that releasing it might be a mistake because i am just bridging it, which means arc still controls that object since its originally a NSMutableData object.

To make matters worse i read that CFRelease(Null) is not good.

I'm super confused, any help would be appreciated.

Edit:

Ok i nslogged the pointers before sending them to the CFRelease, they are there!

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

So back to the original question, how why is the static analyzer telling me that i am sending a null pointer argument? and how do i fix/mute this?

thanks

PD: Is it possible that i am not sending a null pointer but a pointer to null?


Solution

  • What static analyser is telling you is that it's possible for one of the argumenta to be null.

    For example if you place a return after

    NSLog(@"***Could not create image destination ***");
    

    Does the warning stop?