Ios CtruneLegateRef iPhone的内存管理
我专注于一个基于OmniGroup的rtf编辑器添加图像支持的项目。我在使用Ios CtruneLegateRef iPhone的内存管理,ios,memory,core-text,Ios,Memory,Core Text,我专注于一个基于OmniGroup的rtf编辑器添加图像支持的项目。我在使用CTFramesetterCreateWithAttributedString时遇到了这个问题,它收到了 EXC_坏访问 这是由僵尸对象引起的 为简单起见,我从一个只有照片的rtfd文件中获取NSAttributedString。我添加了基于Omni示例在iOS上解析图像标记的方法。创建NSAttributedString的部分后面跟着这个,如下所示: CTRunDelegateCallbacks callba
CTFramesetterCreateWithAttributedString
时遇到了这个问题,它收到了
EXC_坏访问
这是由僵尸对象引起的
为简单起见,我从一个只有照片的rtfd文件中获取NSAttributedString
。我添加了基于Omni示例在iOS上解析图像标记的方法。创建NSAttributedString
的部分后面跟着这个,如下所示:
CTRunDelegateCallbacks callbacks;
callbacks.version = kCTRunDelegateCurrentVersion;
callbacks.getAscent = ascentCallback;
callbacks.getDescent = descentCallback;
callbacks.getWidth = widthCallback;
callbacks.dealloc = deallocCallback;
CTRunDelegateRef delegate = CTRunDelegateCreate(&callbacks, imgAttr);
NSDictionary *imgAttr = [NSDictionary dictionaryWithObjectsAndKeys:imgWidth,kImageWidth,imgHeight,kImageHeight, nil];//recored the width and height
NSDictionary *attrDictionaryDelegate = [NSDictionary dictionaryWithObjectsAndKeys:(id)delegate, (NSString*)kCTRunDelegateAttributeName,nil];
[_attributedString appendString:@" " attributes:attrDictionaryDelegate];
CFRelease(delegate);
此处的appendString:attributes:
由Omni提供,是nsmutableAttributeString
的一个类别:
- (void)appendString:(NSString *)string attributes:(NSDictionary *)attributes;
{
NSAttributedString *append;
append = [[NSAttributedString alloc] initWithString:string attributes:attributes];
[self appendAttributedString:append];
[append release];
}
在解析器类中,我测试了CTFramesetterCreateWithAttributedString
,它成功地创建并没有内存问题。但是,当我在视图类中调用CTFramesetterCreateWithAttributedString
时,它会收到EXC\u BAD\u ACESS
问题。我将CTFramesetterCreateWithAttributedString
发送到viewDidLoad:
NSArray *arr = [OUIRTFReader parseRTFString2:rtfString];
self.editFrame.attributedText = [arr objectAtIndex:0];
//for OUIRTFReader
+ (NSArray *)parseRTFString2:(NSString *)rtfString
{
OUIRTFReader *parser = [[self alloc] _initWithRTFString:rtfString];
NSMutableArray *temp = [NSMutableArray arrayWithCapacity:1];
NSAttributedString *tempAstr = [[NSAttributedString alloc] initWithAttributedString:parser.attributedString];
[temp addObject:tempAstr];
[tempAstr release];
if (parser.zjImageArray) {
[temp addObject:parser.zjImageArray];
}
[parser release];
}
这里editFrame是OmniFramework提供的OUIEditableFrame
的ivar。
之后,在OUIEditableFrame
的布局子视图:
中,CTFramesetterCreateWithAttributedString
失败。使用instrument进行评测时,它指出在以下位置有一个僵尸对象:
NSDictionary *imgAttr = [NSDictionary dictionaryWithObjectsAndKeys:imgWidth,kImageWidth,imgHeight,kImageHeight, nil];
它表明
已将Objective-C消息发送到地址处的解除分配对象(僵尸)
我对核心基础不太熟悉,比如UIKIT或其他。所以我想知道在我的代码中使用CTRunDelegateRef为图像创建属性字符串的方法有什么问题
谢谢
CTRunDelegateCreate
使用任意上下文指针(void*
)创建委托。这意味着字典imgAttr
不由代理保留
创建委托时需要保留上下文词典,并在DealLocal回调中释放它。但是我使用了NSDictionary类方法,DealLocalCallback什么也没做。我使用alloc并实现了DealLocalCallback,但问题不存在。这让我很困惑。无论如何,你是对的!