Open seanm opened 16 years ago
The C standard guarantees that you can safely pass NULL to free() and that nothing happens in response. Likewise for C++'s delete. Likewise for other memory allocation functions, see:
http://developer.apple.com/qa/qa2001/qa1259.html
Consider:
int main(int argc, char argv[]) { char foo = malloc(1024); if (foo) free (foo);
return 0;
}
Sometime people add such checks for null when there is really no need.
It would be nice for the static analyzer to catch such things, perhaps under a 'coding conventions' grouping.
The reverse would be nice too. For example, passing null to CFRelease() is documented to be a no-no. Catching such situations would also be helpful.
int main(int argc, char *argv[]) { CFStringRef str = CFStringCreateWithCString (kCFAllocatorDefault, "foo", kCFStringEncodingASCII); // may return NULL! str = NULL; CFRelease(str);
assigned to @tkremenek
Extended Description
The C standard guarantees that you can safely pass NULL to free() and that nothing happens in response. Likewise for C++'s delete. Likewise for other memory allocation functions, see:
http://developer.apple.com/qa/qa2001/qa1259.html
Consider:
int main(int argc, char argv[]) { char foo = malloc(1024); if (foo) free (foo);
}
Sometime people add such checks for null when there is really no need.
It would be nice for the static analyzer to catch such things, perhaps under a 'coding conventions' grouping.
The reverse would be nice too. For example, passing null to CFRelease() is documented to be a no-no. Catching such situations would also be helpful.
int main(int argc, char *argv[]) { CFStringRef str = CFStringCreateWithCString (kCFAllocatorDefault, "foo", kCFStringEncodingASCII); // may return NULL! str = NULL; CFRelease(str);
}