簡體   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