用于CTRunDelegateRef iPhone的内存管理 [英] memory management for CTRunDelegateRef iPhone
问题描述
我专注于基于OmniGroup的rtf编辑器添加图像支持的项目。当我使用 CTFramesetterCreateWithAttributedString
时遇到了问题,并且它收到了
I focus on a project to add image support based on OmniGroup's rtf editor.I met the problem when I use CTFramesetterCreateWithAttributedString
, and it receives
EXC_BAD_ACCESS
EXC_BAD_ACCESS
由僵尸对象引起。
为简单起见,我从只有照片的rtfd文件中获取 NSAttributedString
。我根据Omni的样本添加了解析iOS上的图像标签的方法。创建 NSAttributedString
的部分之后是来自raywenderlich的教程,看起来像:
For simplicity, I get the NSAttributedString
from a rtfd file which only has a photo. I add the method to parser the image tag on iOS based on the Omni's sample. And the part to create the NSAttributedString
is followed by this tutorial from raywenderlich, and looks like:
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
的类别:
here appendString:attributes:
is provided by Omni and is a category for NSMutableAttributedString
:
- (void)appendString:(NSString *)string attributes:(NSDictionary *)attributes;
{
NSAttributedString *append;
append = [[NSAttributedString alloc] initWithString:string attributes:attributes];
[self appendAttributedString:append];
[append release];
}
在Parser类中我测试 CTFramesetterCreateWithAttributedString
并且它成功创建并且不会发生内存问题。但是,当我在视图类中调用 CTFramesetterCreateWithAttributedString
时,它会收到 EXC_BAD_ACESS
问题。我将 CTFramesetterCreateWithAttributedString
发送到 viewDidLoad中的视图:
In the Parser class I test CTFramesetterCreateWithAttributedString
and it creates successfully and no memory problem happens. However, when I call CTFramesetterCreateWithAttributedString
in my view class, it receives the EXC_BAD_ACESS
problem. I send the CTFramesetterCreateWithAttributedString
to my view in the 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是的OUARdableFrame
由OmniFramework提供。
之后,在 layoutSubview:
的 OUIEditableFrame
中, CTFramesetterCreateWithAttributedString
失败。使用工具进行分析,它指出有一个僵尸对象:
here editFrame is ivar of OUIEditableFrame
provided by OmniFramework.
After that, in the layoutSubview:
of OUIEditableFrame
, CTFramesetterCreateWithAttributedString
failed. Profiling with instrument, it points there is a zombie object in :
NSDictionary *imgAttr = [NSDictionary dictionaryWithObjectsAndKeys:imgWidth,kImageWidth,imgHeight,kImageHeight, nil];
它表明
Objective-C消息被发送到地址
An Objective-C message was sent to a deallocated object(zombie) at address
我不太熟悉像UIKit或其他人一样的核心基础。所以我想知道在我的代码中使用CTRunDelegateRef为图像创建属性字符串的方法有什么问题?
I'm not so familiar with core foundation like as UIKit or others. So I'd like to know what's wrong with the method to create a attributed string for image with CTRunDelegateRef in my code?
谢谢!
推荐答案
CTRunDelegateCreate
创建一个带有任意上下文指针的委托( void *
)。这意味着委托不保留字典 imgAttr
。
CTRunDelegateCreate
create a delegate with an arbitrary context pointer (void *
). That means the dictionary imgAttr
isn't retained by the delegate.
创建时需要保留上下文字典委托并在dealloc回调中释放它。
You need to retain the context dictionary when creating the delegate and release it in the dealloc callback.
这篇关于用于CTRunDelegateRef iPhone的内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!