内存pipe理CTRunDelegateRef iPhone
我专注于一个项目,添加基于OmniGroup的rtf编辑器的图像支持。我遇到了这个问题,当我使用CTFramesetterCreateWithAttributedString
,它接收
EXC_BAD_ACCESS
这是由一个僵尸对象造成的。
为了简单起见,我从只有照片的rtfd文件中获取NSAttributedString
。 我添加了方法来parsing基于Omni的示例iOS上的图像标记。 而且,创buildNSAttributedString
的部分紧接着来自raywenderlich的这个教程 ,如下所示:
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提供,是NSMutableAttributedString
一个类别:
- (void)appendString:(NSString *)string attributes:(NSDictionary *)attributes; { NSAttributedString *append; append = [[NSAttributedString alloc] initWithString:string attributes:attributes]; [self appendAttributedString:append]; [append release]; }
在parsing器类中,我testing了CTFramesetterCreateWithAttributedString
,它创build成功,没有发生内存问题。 但是,当我在我的视图类中调用CTFramesetterCreateWithAttributedString
时,它收到EXC_BAD_ACESS
问题。 我发送CTFramesetterCreateWithAttributedString
到viewDidLoad:
视图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是由OUIEditableFrame
提供的OUIEditableFrame的ivar。 之后,在OUIEditableFrame
的layoutSubview:
中, CTFramesetterCreateWithAttributedString
失败。 用仪器分析,它指出有一个僵尸对象在:
NSDictionary *imgAttr = [NSDictionary dictionaryWithObjectsAndKeys:imgWidth,kImageWidth,imgHeight,kImageHeight, nil];
这certificate了
一个Objective-C消息被发送到一个解除分配的对象(僵尸)的地址
我不太了解像UIKit或其他人那样的核心基础。 所以我想知道什么是错误的方法来创buildCTRunDelegateRef在我的代码图像的属性string?
谢谢!
CTRunDelegateCreate
使用任意上下文指针( void *
)创build一个委托。 这意味着代表字典imgAttr
不被保留。
您需要在创build委托时保留上下文字典,并在dealloccallback中释放它。