CF_RETURNS_RETAINED の紹介ほか。ちょっとした場面で便利。
次のようなコードを Clang Static Analyzer にかけると cgColor が開放されていないと指摘される。
-(CGColorRef)cw_cgColor {
NSColor *nscolor = [self colorUsingColorSpaceName:
NSCalibratedRGBColorSpace];
CGFloat components[4];
[nscolor getRed:&components[0] green:&components[1]
blue:&components[2] alpha:&components[3]];
CGColorSpaceRef space = CGColorSpaceCreateWithName(
kCGColorSpaceGenericRGB);
CGColorRef cgColor = CGColorCreate(space, components);
CGColorSpaceRelease(space);
return cgColor;
}メソッドを呼び出す側での開放を想定している場合はこの指摘を無視したい。そんな時に CF_RETURNS_RETAINED が使える。
@interface NSColor (CWNSColorAdditions)
-(CGColorRef)cw_cgColor CF_RETURNS_RETAINED;
@end
Clang のアノテーションは他にもあって、まとめページが記事で紹介されていた。
引数の NULLチェックを静的に行う為の nonnull なんてものもある。
その他、最初に紹介したような戻り値の開放に関するもの(returns)、引数として渡す時の開放に関するもの(consumed)などがある。
Mac OS X API Annotations
Cocoa & Core Foundation Memory Management Annotations
Attribute 'ns_returns_retained'
Attribute 'ns_returns_not_retained'
Attribute 'cf_returns_retained'
Attribute 'cf_returns_not_retained'
Attribute 'ns_consumed'
Attribute 'cf_consumed'
Attribute 'ns_consumes_self'
Custom Assertion Handlers
Attribute 'noreturn'
Attribute 'analyzer_noreturn'



