@@ -295,15 +296,15 @@ CFDateRef returnsRetainedCFDate() {
@implementation MyClass
- (NSDate*) returnsCFRetained {
- return (NSDate*) returnsRetainedCFDate(); // No leak.
+ return (NSDate*) returnsRetainedCFDate(); // No leak.
}
- (NSDate*) alsoReturnsRetained {
- return (NSDate*) returnsRetainedCFDate(); // Always report a leak.
+ return (NSDate*) returnsRetainedCFDate(); // Always report a leak.
}
- (NSDate*) returnsNSRetained {
- return (NSDate*) returnsRetainedCFDate(); // Report a leak when using GC.
+ return (NSDate*) returnsRetainedCFDate(); // Report a leak when using GC.
}
@end
@@ -351,6 +352,112 @@ its availability, as it is not available in earlier versions of the analyzer:
+
Attribute 'ns_consumed'
+(Clang-specific)
+
+
The 'ns_consumed' attribute can be placed on a specific parameter in either the declaration of a function or an Objective-C method.
+ It indicates to the static analyzer that a release message is implicitly sent to the parameter upon
+ completion of the call to the given function or method.
+
+
Important note when using Garbage Collection: Note that the analyzer
+essentially ignores this attribute when code is compiled to use Objective-C
+garbage collection. This is because the release message does nothing
+when using GC. If the underlying function/method uses something like
+CFRelease to decrement the reference count, consider using
+the cf_consumed attribute instead.
+
+
Example
+
+
+$ cat test.m
+#ifndef __has_feature // Optional.
+#define __has_feature(x) 0 // Compatibility with non-clang compilers.
+#endif
+
+#ifndef NS_CONSUMED
+#if __has_feature(attribute_ns_consumed)
+#define NS_CONSUMED __attribute__((NS_CONSUMED))
+#else
+#define NS_CONSUMED
+#endif
+#endif
+
+void consume_ns(id NS_CONSUMED x);
+
+void test() {
+ id x = [[NSObject alloc] init];
+ consume_ns(x); // No leak!
+}
+
+@interface Foo : NSObject
++ (void) releaseArg:(id) NS_CONSUMED x;
++ (void) releaseSecondArg:(id)x second:(id) NS_CONSUMED y;
+@end
+
+void test_method() {
+ id x = [[NSObject alloc] init];
+ [Foo releaseArg:x]; // No leak!
+}
+
+void test_method2() {
+ id a = [[NSObject alloc] init];
+ id b = [[NSObject alloc] init];
+ [Foo releaseSecondArg:a second:b]; // 'a' is leaked, but 'b' is released.
+}
+
+
+
Attribute 'cf_consumed'
+(Clang-specific)
+
+
The 'cf_consumed' attribute is practically identical to ns_consumed.
+The attribute can be placed on a specific parameter in either the declaration of a function or an Objective-C method.
+It indicates to the static analyzer that the object reference is implicitly passed to a call to CFRelease upon
+completion of the call to the given function or method.
+
+
Operationally this attribute is nearly identical to ns_consumed
+with the main difference that the reference count decrement still occurs when using Objective-C garbage
+collection (which is import for Core Foundation types, which are not automatically garbage collected).