ARC 中的大量内存使用

Huge memory usage in ARC

我在绘制 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;

}

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

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

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;

}

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