2011-12-19 10 views
1

私はCTFramesetterCreateWithAttributedStringを使用する場合editor.Iが問題に会ったOmniGroupのRTF形式に基づいて画像のサポートを追加するプロジェクトに焦点を当て、それがあるメモリ管理CTRunDelegateRef iPhone

EXC_BAD_ACCESS

を受けるためゾンビオブジェクトに起因します。

簡単にするために、私はNSAttributedStringを写真のみのrtfdファイルから入手します。オムニのサンプルに基づいて、iOS上に画像タグを解析するメソッドを追加します。そしてNSAttributedStringを作成するための部分が、このtutorial from raywenderlichが続く、とのように見えるされています。ここappendString:attributes:


    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); 

オムニによって提供されており、NSMutableAttributedStringのカテゴリである:私はテストParserクラスで


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

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

CTFramesetterCreateWithAttributedStringと正常に作成され、メモリの問題は発生しません。しかし、ビュークラスでCTFramesetterCreateWithAttributedStringと呼ぶと、EXC_BAD_ACESSという問題が発生します。私はここeditFrameがOmniFrameworkによって提供さOUIEditableFrameのIVARある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]; 
} 

に私の見解にCTFramesetterCreateWithAttributedStringを送ります。 その後、layoutSubview:OUIEditableFrameには、CTFramesetterCreateWithAttributedStringが失敗しました。これは

Objective-Cのメッセージがアドレス

に割り当て解除オブジェクト(ゾンビ)に送信されたことを実証


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

:器具とプロファイリング、それにゾンビオブジェクトがある点

私はUIKitやその他のようなコアファンデーションに精通していません。だから、私のコードでCTRunDelegateRefを使ってイメージの属性付き文字列を作成する方法に何が問題なのかを知りたいのですが?

ありがとうございます!

答えて

0

CTRunDelegateCreate任意のコンテキストポインタ(void *)でデリゲートを作成します。つまり、辞書imgAttrはデリゲートによって保持されません。

デリゲートを作成するときにコンテキスト辞書を保持し、deallocコールバックで解放する必要があります。

+0

しかし、私はNSDictionaryクラスメソッドを使用し、deallocCallbackは何もしませんでした。 – scorpiozj

+0

私はallocを使用してdeallocCallbackを実装していますが、問題はありません。それは私を混乱させます。とにかく、あなたは正しいです! – scorpiozj