2017-11-06 4 views
1

내가이 NSView의 그릴 스레드에서 호출 기능이있다 : 올바르게 할당 해제 및 I되었다 이러한 기능에서 만든거대한 메모리 사용

+(NSAttributedString*) createCurrentTextWithString:(NSString *)string AndMaxLenght:(float)length AndMaxHeight:(float)maxHeight AndColor:(NSColor *)color AndFontName: (NSString*) fontName 
{ 


float dim=0.1; 

NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:[CustomFont customFontWithName:fontName AndSize:dim], NSFontAttributeName,color, NSForegroundColorAttributeName, nil]; 

NSAttributedString * currentText=[[NSAttributedString alloc] initWithString:string attributes: dictionary]; 


while([currentText size].width<maxLength&&[currentText size].height<maxHeight) 
{ 

    dictionary=[NSDictionary dictionaryWithObjectsAndKeys:[CustomFont customFontWithName:fontName AndSize:dim], NSFontAttributeName,color, NSForegroundColorAttributeName, nil]; 

    currentText=[[NSAttributedString alloc] initWithString:string attributes: retval]; 


    dim+=0.1; 

} 

return currentText; 

} 

모든 제품 :

+(NSFont *)customFontWithName:(NSString *)fontName AndSize:(float)fontSize 
{ 

NSData *data = [[[NSDataAsset alloc]initWithName:fontName] data]; 
CGDataProviderRef fontProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data); 

CGFontRef cgFont = CGFontCreateWithDataProvider(fontProvider); 
CGDataProviderRelease(fontProvider); 
NSDictionary *fontsizeAttr=[NSDictionary dictionaryWithObjectsAndKeys: 
          [NSNumber numberWithFloat:fontSize], NSFontSizeAttribute, 
          nil]; 
CTFontDescriptorRef fontDescriptor = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)fontsizeAttr); 
CTFontRef font = CTFontCreateWithGraphicsFont(cgFont, 0, NULL, fontDescriptor); 
CFRelease(fontDescriptor); 
CGFontRelease(cgFont); 

NSFont* retval= (__bridge NSFont*)font; 
CFRelease(font); 
return retval; 
} 

이를 메모리 누수를 찾을 수 없지만이 코드는 메모리를 많이 사용하여 (수 기가 바이트) 원인을 이해할 수 없습니다. 도와주세요.

+0

현재 텍스트가 너무 큽니까? 반환하려면 - (__bridge NSFont *) 글꼴 - NSFont를 사용하는 대신 직접 수행 –

+1

자동 릴리스 된 개체를 피하십시오. '[NSDictionary dictionaryWithObjectsAndKeys :] '대신'[[NSDictionary alloc] initWithObjectsAndKeys :]'를 사용하십시오. 그리고/또는 루프에서'@autoreleasepool {}'을 사용하십시오. – Willeke

답변

0

해결책을 찾았습니다. 어떤 이유에서인지 모르겠지만 코드는 다음과 같습니다.

NSData *data = [[[NSDataAsset alloc]initWithName:fontName] data]; 
CGDataProviderRef fontProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data); 
CGFontRef cgFont = CGFontCreateWithDataProvider(fontProvider); 

allocs 더 이상 할당을 해제하지 않을 큰 메모리를 할당합니다. 그래서 나는 각 사용자 정의 글꼴에 대해 정적 변수 CGFontRef를 만듭니다. 이것은 내가 찾을 수있는 유일한 방법입니다 :

static CGFontRef font1; 
.... 
static CGFontRef font; 

+(CGFontRef) getFontWithValue: (int) value 
{ 
switch (value) 
{ 
    case 1: 
     return font1; 
     break; 
     ... 
    case n: 
     return fontn; 
    default: 
     return NULL; 
} 
} 

그리고

+(NSFont*) customFontWithName:(int)fontName AndSize:(float)fontSize 
{ 
NSDictionary *fontsizeAttr=[NSDictionary dictionaryWithObjectsAndKeys: 
          [NSNumber numberWithFloat:fontSize], NSFontSizeAttribute, 
          nil]; 
CTFontDescriptorRef fontDescriptor = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)fontsizeAttr); 
CTFontRef font = CTFontCreateWithGraphicsFont([CustomFont getFontWithValue:fontName], 0, NULL, fontDescriptor); 
CFRelease(fontDescriptor); 
NSFont* retval= (__bridge NSFont*)font; 
CFRelease(font); 
return retval; 

} 

가이 메모리 누수이며,이 솔루션,하지만 트릭없는 이유를 난 아직도 이해하지 않지만, 작동 .

관련 문제