2011-12-19 134 views
1

我专注于一个项目的基础上添加OmniGroup的RTF图像支持editor.I遇到的问题,当我使用CTFramesetterCreateWithAttributedString,它接收内存管理CTRunDelegateRef iPhone

EXC_BAD_ACCESS

这是由僵尸对象造成的。

为了简单起见,我从仅包含照片的rtfd文件中获取NSAttributedString。我添加了方法来根据Omni的示例解析iOS上的图像标记。并以创建NSAttributedString的部分之后该tutorial from 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:通过全方位提供,是NSMutableAttributedString类别:


- (void)appendString:(NSString *)string attributes:(NSDictionary *)attributes; 
{ 
    NSAttributedString *append; 

    append = [[NSAttributedString alloc] initWithString:string attributes:attributes]; 
    [self appendAttributedString:append]; 
    [append release]; 
} 

在解析器I类测试CTFramesetterCreateWithAttributedString并且它创建成功并且不会发生内存问题。但是,当我在我的视图类中调用CTFramesetterCreateWithAttributedString时,它收到EXC_BAD_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伊娃。 之后,在的OUIEditableFrame,CTFramesetterCreateWithAttributedString失败。用仪器分析,它指向有一个僵尸对象中:


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

它表明

一个Objective-C消息被在地址发送到解除分配的对象(僵尸)

我并不像UIKit或其他人那样熟悉核心基础。所以我想知道在我的代码中使用CTRunDelegateRef为图像创建属性字符串的方法有什么问题?

谢谢!

回答

0

CTRunDelegateCreate使用任意上下文指针创建委托(void *)。这意味着代表不会保留字典imgAttr

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

+0

但我用NSDictionary类的方法和deallocCallback什么也没做。 – scorpiozj 2011-12-20 00:58:44

+0

我使用alloc并执行deallocCallback,并且缺少问题。这让我感到困惑。无论如何,你是对的! – scorpiozj 2011-12-20 00:59:59