• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: memory management (perhaps naive) question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: memory management (perhaps naive) question


  • Subject: Re: memory management (perhaps naive) question
  • From: Evan Schoenberg <email@hidden>
  • Date: Tue, 9 Nov 2004 16:59:42 -0600

Apologies, I was thinking of NSFont's fontWithName:, which feels like it should be part of NSFontManager :)

Here's a method from Adium's AIFontAdditions NSFont category (GPL code) which attempts
to work around the fontWithName: leaking.


//Returns the requested font
//NSFont's 'FontWithName' method leaks memory. This wrapper attempts to minimize the leaking.
//It appears to be leaking an NSString somehow, which isn't as bad as what NSFont does.
+ (NSFont *)cachedFontWithName:(NSString *)fontName size:(float)fontSize
{
static NSMutableDictionary *fontDict = nil;
NSString *sizeString = [NSString stringWithFormat:@"%0.2f",fontSize];
NSMutableDictionary *sizeDict = nil;
NSFont *font = nil;


    if(!fontDict){
        fontDict = [[NSMutableDictionary alloc] init];
    }

    if (fontName){
        sizeDict = [fontDict objectForKey:fontName];
        if(!sizeDict){
            sizeDict = [NSMutableDictionary dictionary];
            [fontDict setObject:sizeDict forKey:fontName];
        }

        font = [sizeDict objectForKey:sizeString];

        if(!font){
            font = [NSFont fontWithName:fontName size:fontSize];

//If the font doesn't exist on the system, use the controlContentFont
if (!font){
font = [NSFont controlContentFontOfSize:fontSize];
NSAssert(font != nil, @"controlContentFont not found.");
}


            [sizeDict setObject:font
                        forKey:sizeString];
            [fontDict setObject:sizeDict forKey:fontName];
        }
    }else{
        //Use the control content font if we are passed a nil fontName
        font = [NSFont controlContentFontOfSize:fontSize];
        fontName = [font fontName];

        sizeDict = [fontDict objectForKey:fontName];
        if(!sizeDict){
            sizeDict = [NSMutableDictionary dictionary];
        }

        [sizeDict setObject:font
                     forKey:sizeString];
        [fontDict setObject:sizeDict forKey:fontName];
    }

    return(font);
}

-Evan
www.adiumx.com

On Nov 9, 2004, at 3:20 AM, Jonathon Mah wrote:


On 9 Nov 2004, at 13:20, Evan Schoenberg wrote:

Apple's NSFontManager also has some leaks which caching can minimize but not completely avoid.

I'm using NSFontManager in my application (it's not finished yet, so I haven't started testing for leaks). Could you please elaborate (or point me to somewhere with more info)? Thanks!



Jonathon Mah email@hidden



_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Re: memory management (perhaps naive) question (From: Scott Ribe <email@hidden>)
 >Re: memory management (perhaps naive) question (From: The Karl Adam <email@hidden>)
 >Re: memory management (perhaps naive) question (From: Evan Schoenberg <email@hidden>)
 >Re: memory management (perhaps naive) question (From: Jonathon Mah <email@hidden>)

  • Prev by Date: Re: New Spotlight Info
  • Next by Date: Re: Nibs and memory leaks
  • Previous by thread: Re: memory management (perhaps naive) question
  • Next by thread: Re: how to get the iTunes look (a related question)
  • Index(es):
    • Date
    • Thread