0
下面是一些示例代码:如何正确使用CFRetain和CFRelease?
@interface Foo : NSObject
{
CFAttributedStringRef m_foo;
}
@property (nonatomic, assign) CFAttributedStringRef foo;
@end
@implementation Foo
@synthesize foo = m_foo;
- (id)initWithAttributedString:(CFAttributedStringRef)attributedString
{
self = [super init];
if (self == nil)
return nil;
if (attributedString != NULL)
{
self.foo = CFAttributedStringCreateCopy(NULL, attributedString);
}
return self;
}
- (void)dealloc
{
if (self.foo != NULL)
CFRelease(self.foo);
[super dealloc];
}
@end
的XCode警告我,潜在的内存泄漏在CFAttributedStringCreateCopy
和CFRelease
两者。为什么?
编辑:如果我直接使用成员变量m_foo
它纠正了问题。我有财产的内存管理语义错误吗?
您还可以使用ARC和免费桥接将其直接转换为NSAttributedString并为您管理内存 – SomeGuy