Skip to content

Conversation

@ninjaprox
Copy link

No description provided.

@jamesstout
Copy link

There are a few other leaks from this initialize method: the bundle, url and array

screen shot 2017-03-27 at 1 55 20 am
screen shot 2017-03-27 at 1 54 36 am

Moving the bundle and array above the dispatch_once call eliminates the leaks:

+ (void) initialize {
    [super initialize];
    static dispatch_once_t onceToken;
    NSArray *fontNames = @[@"Lato-Regular", @"Lato-Bold", @"Lato-Italic", @"Lato-Light"];
    NSBundle *mainBundle = [NSBundle mainBundle];
    dispatch_once(&onceToken, ^{
      for (NSString *fontName in fontNames) {
        NSURL * url = [mainBundle URLForResource:fontName withExtension:@"ttf"];
        if (url) {
          CFErrorRef error;
          CTFontManagerRegisterFontsForURL((__bridge CFURLRef)url, kCTFontManagerScopeNone, &error);
          CFRelease(error);
        }
      }
  });
}

However, I thought initialize was only ever called once, but it's called twice (in my code at least), so a guard is needed (?):

+ (void) initialize {
    [super initialize];
    if (self == [UIFont self]) {
      static dispatch_once_t onceToken;
      NSArray *fontNames = @[@"Lato-Regular", @"Lato-Bold", @"Lato-Italic", @"Lato-Light"];
      NSBundle *mainBundle = [NSBundle mainBundle];
      dispatch_once(&onceToken, ^{
        for (NSString *fontName in fontNames) {
          NSURL * url = [mainBundle URLForResource:fontName withExtension:@"ttf"];
          if (url) {
            CFErrorRef error;
            CTFontManagerRegisterFontsForURL((__bridge CFURLRef)url, kCTFontManagerScopeNone, &error);
            CFRelease(error);
          }
        }
    });
  }
}

I'm not sure about the guard part, maybe initialize is required to be called twice.

Anyway, the first code eliminates the leaks, small as they are (around 1.4kb in my code)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants