2012-02-16 4 views
2

J'utilise la méthode suivante pour dessiner du texte sur un document PDF (contexte). D'une certaine manière instruments vient avec la ligne suivante fuiteFuite de mémoire dans CFAttributedStringRef

CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL); 

Où je suis libérer le stringRef. Voici le code (Après les bonnes réponses voici la mise à jour/code de travail):

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

CFStringRef stringRef = (__bridge CFStringRef)textToDraw; 
// Prepare the text using a Core Text Framesetter 
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL); 
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText); 

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

// 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); The string shouldn't be released 
CFRelease(framesetter); 
//Added the next line: 
CFRelease(currentText); 

}

testé sur un dispositif que le simulateur; il fuit

Répondre

2

Vous devez également libérer le CFAttributedStringRef et je ne vois pas que vous l'avez publié dans la méthode. La propriété dans cette fonction suit la règle

créer

https://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html#//apple_ref/doc/uid/20001148-103029

+0

Merci pour la réponse rapide. Lorsque j'ajoute CFRelease (currentText) mon application se bloque .. mmm – Oritm

+0

pouvez-vous poster l'erreur? C'est probablement ce que vous faites quand on y accède par quelque chose d'autre (comme un framesetter, par exemple). –

+0

il se bloque juste, mais quand j'ai NSZombies activé, il arrive avec: *** - [CFString release]: message envoyé à l'instance désaffectée 0xf6534c0 – Oritm