2013-02-04 2 views
1

Je génère un PDF en utilisant le code ci-dessous, mais cela conduit à une fuite de mémoire peut-on aider? le code est donné ci-dessous.Memoryleak en raison de CTFontRef

- (void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect { 


    NSMutableAttributedString *string = [[[NSMutableAttributedString alloc] 
             initWithString:textToDraw] autorelease]; 

    // make a few words bold 

    CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 8.0, NULL); 

    [string addAttribute:(id)kCTFontAttributeName 
        value:(id)helveticaBold 
        range:NSMakeRange(0, [string length])]; 

    // add some color. 
    if (_flag == 1) { 

     [string addAttribute:(id)kCTForegroundColorAttributeName 
         value:(id)[UIColor whiteColor].CGColor 
         range:NSMakeRange(0, [string length])]; 


    } else { 

     [string addAttribute:(id)kCTForegroundColorAttributeName 
         value:(id)[UIColor blackColor].CGColor 
         range:NSMakeRange(0, [string length])]; 
    } 

    // layout master 
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string); 

    CGMutablePathRef framePath = CGPathCreateMutable(); 
    CGPathAddRect(framePath, NULL, frameRect); 

    // Get the frame that will do the rendering. 
    CFRange currentRange = CFRangeMake(0, 0); 
    CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL); 
    CGPathRelease(framePath); 

    // Get the graphics context. 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

    // Put the text matrix into a known state. This ensures 
    // that no old scaling factors are left in place. 
    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity); 
    CGContextSetRGBFillColor(currentContext, 0, 0, 0, 1.0); 

    // Core Text draws from the bottom-left corner up, so flip 
    // the current transform prior to drawing. 
    CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2); 
    CGContextScaleCTM(currentContext, 1.0, -1.0); 

    // Draw the frame. 
    CTFrameDraw(frameRef, currentContext); 

    CGContextScaleCTM(currentContext, 1.0, -1.0); 
    CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2); 

    CFRelease(frameRef); 
    //CFRelease(stringRef); 
    CFRelease(framesetter); 

} 

J'appelle cette fonction plusieurs fois alors que la production du

PDF et chaque fois que cela conduit à la fuite de mémoire.

Répondre

6

CTFontCreateWithName suit la création nom règle, qui est si vous créez, vous possédez et vous devez le libérer lorsque vous avez terminé:

CFRelease(helveticaBold); 
+0

its me avertissant que la déclaration implicite de la fonction invalide dans C99 quelque chose. –

+0

Désolé, mon erreur. Utilisez simplement CFRelease. –

+0

Savez-vous que vous êtes génial;) il a juste travaillé Parfait merci 4 ce grand Aide Cheers: P –

Questions connexes