Pointeur nul dans CFRelease (indiqué par l'anylizer statique)
-
16-11-2019 - |
Question
J'ai ce 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);
}
Mais lorsque je lance l'analyseur statique, il me dit que :
Argument de pointeur nul dans l'appel à CFRelease
Mais selon ma logique, je devrais publier le CGImageSourceRef qui est créé par un CGImageDestinationCreateWithData.
Je pense également que le publier pourrait être une erreur car je ne fais que le combler, ce qui signifie que l'arc contrôle toujours cet objet puisqu'il s'agit à l'origine d'un objet. NSMutableData objet.
Pour aggraver les choses, j'ai lu que CFRelease(Null) n'est pas bon.
Je suis super confus, toute aide serait appréciée.
Modifier:
Ok j'ai enregistré les pointeurs avant de les envoyer au CFRelease, ils sont là !
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]>
Revenons donc à la question initiale, pourquoi l'analyseur statique me dit-il que j'envoie un argument de pointeur nul ?et comment puis-je résoudre/couper cela ?
merci
PD :Est-il possible que je n'envoie pas un pointeur nul mais un pointeur vers null ?
La solution
Ce que l'analyseur statique vous dit, c'est que c'est possible pour que l'un des arguments soit nul.
Par exemple, si vous effectuez un retour après
NSLog(@"***Could not create image destination ***");
L'avertissement s'arrête-t-il ?
Autres conseils
CFRelease plantera si vous transmettez un NULL, donc l'analyseur vous avertit que la destination et la source pourraient y être NULL.Ajoutez simplement une vérification pour NULL :
if ( destination != NULL )
CFRelease(destination);
if ( source != NULL )
CFRelease(source);