ARC中的内存使用量很大

时间:2017-11-06 09:29:26

标签: objective-c xcode automatic-ref-counting

我在绘制NSView的线程中调用了这些函数:

+(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;
}

和此:

+(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;

}

在这些函数中创建的所有对象都被正确释放,我无法找到内存泄漏,但是这段代码导致了大量的内存使用(许多千兆字节),我无法理解为什么。请帮忙。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。由于某些原因,我不知道,代码:

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

分配不再释放的大内存。所以我为每个自定义字体创建一个静态变量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;

}

我仍然不明白为什么会出现这种内存泄漏,这不是一个解决方案,而只是一个技巧,但它确实有效。