Nullzeiger in CFRelease (von statischem Anylizer erzählt)
-
16-11-2019 - |
Frage
Ich habe diesen Code bekommen:
- (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);
}
Aber wenn ich den statischen Analysator starte, sagt er mir Folgendes:
Nullzeigerargument im Aufruf von CFRelease
Aber meiner Logik nach sollte ich die CGImageSourceRef veröffentlichen, die von a erstellt wird CGImageDestinationCreateWithData.
Außerdem denke ich, dass es ein Fehler sein könnte, es freizugeben, weil ich es nur überbrücke, was bedeutet, dass Arc dieses Objekt immer noch kontrolliert, da es ursprünglich ein war NSMutableData Objekt.
Um die Sache noch schlimmer zu machen, habe ich gelesen, dass CFRelease(Null) nicht gut ist.
Ich bin total verwirrt, ich wäre für jede Hilfe dankbar.
Bearbeiten:
Ok, ich habe die Zeiger protokolliert, bevor ich sie an CFRelease gesendet habe, sie sind da!
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]>
Also zurück zur ursprünglichen Frage: Wie warum sagt mir der statische Analysator, dass ich ein Nullzeiger-Argument sende?Und wie kann ich das beheben/stummschalten?
Danke
PD:Ist es möglich, dass ich keinen Nullzeiger, sondern einen Zeiger auf Null sende?
Lösung
Der statische Analysator sagt Ihnen, dass dies der Fall ist möglich damit eines der Argumente null ist.
Zum Beispiel, wenn Sie eine Retoure hinterher platzieren
NSLog(@"***Could not create image destination ***");
Hört die Warnung auf?
Andere Tipps
CFRelease stürzt ab, wenn Sie einen NULL-Wert übergeben. Der Analysator warnt Sie daher, dass Ziel und Quelle dort möglicherweise NULL sind.Fügen Sie einfach eine Prüfung auf NULL hinzu:
if ( destination != NULL )
CFRelease(destination);
if ( source != NULL )
CFRelease(source);