简体   繁体   English

CTRunDelegateRef iPhone的内存管理

[英]memory management for CTRunDelegateRef iPhone

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 我专注于一个基于OmniGroup rtf编辑器添加图像支持的项目。当我使用CTFramesetterCreateWithAttributedString时遇到了问题,它收到了

EXC_BAD_ACCESS EXC_BAD_ACCESS

which is caused by a zombie object. 这是由僵尸造成的。

For simplicity, I get the NSAttributedString from a rtfd file which only has a photo. 为简单起见,我从仅带照片的rtfd文件中获取NSAttributedString I add the method to parser the image tag on iOS based on the Omni's sample. 我基于Omni的示例添加了在iOS上解析image标签的方法。 And the part to create the NSAttributedString is followed by this tutorial from raywenderlich , and looks like: NSAttributedString的本教程紧随创建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);

here appendString:attributes: is provided by Omni and is a category for NSMutableAttributedString : 此处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];
}

In the Parser class I test CTFramesetterCreateWithAttributedString and it creates successfully and no memory problem happens. 在Parser类中,我测试了CTFramesetterCreateWithAttributedString ,它创建成功并且没有发生内存问题。 However, when I call CTFramesetterCreateWithAttributedString in my view class, it receives the EXC_BAD_ACESS problem. 但是,当我在视图类中调用CTFramesetterCreateWithAttributedString时,它会收到EXC_BAD_ACESS问题。 I send the CTFramesetterCreateWithAttributedString to my view in the viewDidLoad: 我将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];
}

here editFrame is ivar of OUIEditableFrame provided by OmniFramework. 这里的editFrame是OUIEditableFrame提供的OUIEditableFrame的ivar。 After that, in the layoutSubview: of OUIEditableFrame , CTFramesetterCreateWithAttributedString failed. 之后,在OUIEditableFramelayoutSubview:中, CTFramesetterCreateWithAttributedString失败。 Profiling with instrument, it points there is a zombie object in : 用仪器进行分析,它指出:中有一个僵尸对象:


NSDictionary *imgAttr = [NSDictionary dictionaryWithObjectsAndKeys:imgWidth,kImageWidth,imgHeight,kImageHeight, nil];

It demonstrates that 它表明

An Objective-C message was sent to a deallocated object(zombie) at address 已将Objective-C消息发送到地址为已释放的对象(僵尸)

I'm not so familiar with core foundation like as UIKit or others. 我对UIKit等核心基础不太熟悉。 So I'd like to know what's wrong with the method to create a attributed string for image with CTRunDelegateRef in my code? 因此,我想知道在我的代码中使用CTRunDelegateRef为图像创建属性字符串的方法有什么问题?

Thanks! 谢谢!

CTRunDelegateCreate create a delegate with an arbitrary context pointer ( void * ). CTRunDelegateCreate使用任意上下文指针( void * )创建一个委托。 That means the dictionary imgAttr isn't retained by the delegate. 这意味着字典imgAttr没有被委托保留。

You need to retain the context dictionary when creating the delegate and release it in the dealloc callback. 创建委托时,您需要保留上下文字典,并在dealloc回调中释放它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM