繁体   English   中英

CTRunDelegateRef iPhone的内存管理

[英]memory management for CTRunDelegateRef iPhone

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

EXC_BAD_ACCESS

这是由僵尸造成的。

为简单起见,我从仅带照片的rtfd文件中获取NSAttributedString 我基于Omni的示例添加了在iOS上解析image标签的方法。 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);

此处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];
}

在Parser类中,我测试了CTFramesetterCreateWithAttributedString ,它创建成功并且没有发生内存问题。 但是,当我在视图类中调用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。 之后,在OUIEditableFramelayoutSubview:中, CTFramesetterCreateWithAttributedString失败。 用仪器进行分析,它指出:中有一个僵尸对象:


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

它表明

已将Objective-C消息发送到地址为已释放的对象(僵尸)

我对UIKit等核心基础不太熟悉。 因此,我想知道在我的代码中使用CTRunDelegateRef为图像创建属性字符串的方法有什么问题?

谢谢!

CTRunDelegateCreate使用任意上下文指针( void * )创建一个委托。 这意味着字典imgAttr没有被委托保留。

创建委托时,您需要保留上下文字典,并在dealloc回调中释放它。

暂无
暂无

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

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