我有类似于下面的CFObject返回方法
-(CFMutableAttributedStringRef)getAttStrForArray:(NSArray*)substrings forString:(NSString*)aStr
{
CFMutableAttributedStringRef as3 = CFAttributedStringCreateMutable(NULL, 0);
CFAttributedStringBeginEditing(as3);
CTFontRef font = CTFontCreateWithName(CFSTR("HelveticaNeue-Bold"), 12.5, NULL);
CFAttributedStringReplaceString(as3, CFRangeMake(0, 0), (__bridge CFStringRef)aStr);
......
CFAttributedStringEndEditing(as3);
//CFRelease(as3);
return as3;
}
这里我得到了内存泄漏,我没有释放CFRelease(as3);
对象,因为它是一个返回方法。有没有办法解决内存泄漏问题?
答案 0 :(得分:1)
幸运的是,NSAttributedString
和NSMutableAttributedString
分别与其核心基金会同行CFAttributedStringRef
和CFMutableAttributedStringRef
免费桥接。这意味着您可以创建一个CFAttributedStringRef
并简单地将其转换为NSAttributedString
指针,然后调用NSAttributedString
方法就可以了。
所以只需将桥梁投射到圆弧
NSAttributedString *nsString = (__bridge transfer NSAttributedString*)as3
-(NSMutableAttributedString*)getAttStrForArray:(NSArray*)substrings forString:(NSString*)aStr
{
CFMutableAttributedStringRef as3 = CFAttributedStringCreateMutable(NULL, 0);
//...
return (__bridge transfer NSAttributedString*)as3;
}