From c9acccaddc8bc604bf54bebc4090e5d049caa8d4 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 28 Nov 2017 16:18:07 +0900 Subject: [PATCH] Fix memory leak in ANSCI parsing code We do not have ARC enabled. --- atom/browser/ui/cocoa/NSColor+Hex.h | 4 +- atom/browser/ui/cocoa/NSColor+Hex.mm | 1 - atom/browser/ui/cocoa/NSString+ANSI.h | 2 - atom/browser/ui/cocoa/NSString+ANSI.mm | 26 ++++++++----- atom/browser/ui/tray_icon_cocoa.mm | 54 +++++++++++++++----------- 5 files changed, 49 insertions(+), 38 deletions(-) diff --git a/atom/browser/ui/cocoa/NSColor+Hex.h b/atom/browser/ui/cocoa/NSColor+Hex.h index e2fc344a7ec..70847b42173 100644 --- a/atom/browser/ui/cocoa/NSColor+Hex.h +++ b/atom/browser/ui/cocoa/NSColor+Hex.h @@ -4,8 +4,8 @@ // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. -#ifndef ATOM_BROWSER_UI_COCOA_NSCOLOR+HEX_H_ -#define ATOM_BROWSER_UI_COCOA_NSCOLOR+HEX_H_ +#ifndef ATOM_BROWSER_UI_COCOA_NSCOLOR_HEX_H_ +#define ATOM_BROWSER_UI_COCOA_NSCOLOR_HEX_H_ #import diff --git a/atom/browser/ui/cocoa/NSColor+Hex.mm b/atom/browser/ui/cocoa/NSColor+Hex.mm index a4cf9bc5ee2..b55447ca7e2 100644 --- a/atom/browser/ui/cocoa/NSColor+Hex.mm +++ b/atom/browser/ui/cocoa/NSColor+Hex.mm @@ -9,7 +9,6 @@ @implementation NSColor (Hex) + (NSColor*)colorWithHexColorString:(NSString*)inColorString { - NSColor* result = nil; unsigned colorCode = 0; unsigned char redByte, greenByte, blueByte; diff --git a/atom/browser/ui/cocoa/NSString+ANSI.h b/atom/browser/ui/cocoa/NSString+ANSI.h index 0a038863102..93e76b59e9b 100644 --- a/atom/browser/ui/cocoa/NSString+ANSI.h +++ b/atom/browser/ui/cocoa/NSString+ANSI.h @@ -10,10 +10,8 @@ #import @interface NSString(ANSI) - - (BOOL)containsANSICodes; - (NSMutableAttributedString*)attributedStringParsingANSICodes; - @end #endif // ATOM_BROWSER_UI_COCOA_NSSTRING_ANSI_H_ diff --git a/atom/browser/ui/cocoa/NSString+ANSI.mm b/atom/browser/ui/cocoa/NSString+ANSI.mm index 47d6e9e17d5..3a8250dde94 100644 --- a/atom/browser/ui/cocoa/NSString+ANSI.mm +++ b/atom/browser/ui/cocoa/NSString+ANSI.mm @@ -4,9 +4,9 @@ // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. -#import "Cocoa/Cocoa.h" -#import "NSString+ANSI.h" -#import "NSColor+Hex.h" +#include "atom/browser/ui/cocoa/NSString+ANSI.h" +#include "atom/browser/ui/cocoa/NSColor+Hex.h" +#include "base/mac/scoped_nsobject.h" @implementation NSMutableDictionary (ANSI) @@ -112,9 +112,12 @@ - (NSMutableAttributedString*)attributedStringParsingANSICodes { NSMutableAttributedString* result = [[NSMutableAttributedString alloc] init]; - NSMutableDictionary* attributes = [NSMutableDictionary.alloc init]; + base::scoped_nsobject attributes( + [[NSMutableDictionary alloc] init]); NSArray* parts = [self componentsSeparatedByString:@"\\033["]; - [result appendAttributedString:[NSAttributedString.alloc initWithString:parts.firstObject attributes:nil]]; + [result appendAttributedString:[[[NSAttributedString alloc] + initWithString:parts.firstObject + attributes:nil] autorelease]]; for (NSString* part in [parts subarrayWithRange:NSMakeRange(1, parts.count - 1)]) { if (part.length == 0) @@ -124,17 +127,20 @@ NSString* text = sequence.lastObject; if (sequence.count < 2) { - [result appendAttributedString:[NSAttributedString.alloc initWithString:text attributes:attributes]]; + [result appendAttributedString:[[[NSAttributedString alloc] + initWithString:text + attributes:attributes] autorelease]]; } else if (sequence.count >= 2) { - text = [[sequence subarrayWithRange:NSMakeRange(1, sequence.count - 1)] componentsJoinedByString:@"m"]; + text = [[sequence subarrayWithRange:NSMakeRange(1, sequence.count - 1)] + componentsJoinedByString:@"m"]; [attributes modifyAttributesForANSICodes:sequence[0]]; - [result appendAttributedString:[NSAttributedString.alloc initWithString:text attributes:attributes]]; + [result appendAttributedString:[[[NSAttributedString alloc] + initWithString:text + attributes:attributes] autorelease]]; } } return result; } - - @end diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index d8ff1c4d76c..0554a368381 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -5,12 +5,12 @@ #include "atom/browser/ui/tray_icon_cocoa.h" #include "atom/browser/ui/cocoa/atom_menu_controller.h" +#include "atom/browser/ui/cocoa/NSString+ANSI.h" #include "base/strings/sys_string_conversions.h" #include "ui/events/cocoa/cocoa_event_utils.h" #include "ui/gfx/image/image.h" #include "ui/gfx/mac/coordinate_conversion.h" #include "ui/display/screen.h" -#include "atom/browser/ui/cocoa/NSString+ANSI.h" namespace { @@ -122,8 +122,8 @@ const CGFloat kVerticalTitleMargin = 2; NSRect titleDrawRect = NSMakeRect( [self iconWidth], -kVerticalTitleMargin, [self titleWidth], thickness); - NSAttributedString* titleParsed = [self attributedTitleWithParams:title_]; - + base::scoped_nsobject titleParsed( + [self attributedTitleWithParams:title_]); [titleParsed drawInRect:titleDrawRect]; } } @@ -174,10 +174,13 @@ const CGFloat kVerticalTitleMargin = 2; // The width of the title. - (CGFloat)titleWidth { if (!title_) - return 0; + return 0; - if (ANSI_) - return [[title_ attributedStringParsingANSICodes] size].width + 10; + if (ANSI_) { + base::scoped_nsobject attributedTitle( + [title_ attributedStringParsingANSICodes]); + return [attributedTitle size].width + 10; + } base::scoped_nsobject attributes( [[NSAttributedString alloc] initWithString:title_ @@ -226,25 +229,30 @@ const CGFloat kVerticalTitleMargin = 2; } -- (NSAttributedString*) attributedTitleWithParams:(NSString *)fullTitle { - fullTitle = [fullTitle stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet]; - NSString * title = fullTitle; +- (NSAttributedString*)attributedTitleWithParams:(NSString*)fullTitle { + NSString* title = [fullTitle + stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; + NSDictionary* attributes = + [self titleAttributesWithHighlight:[self isHighlighted]]; - NSDictionary* attributes = [self titleAttributesWithHighlight:[self isHighlighted]]; - NSMutableAttributedString * attributedTitle = [NSMutableAttributedString.alloc initWithString:title attributes:attributes]; - if (ANSI_) { - attributedTitle = [title attributedStringParsingANSICodes]; - [attributedTitle addAttributes:attributes range:NSMakeRange(0, attributedTitle.length)]; - return attributedTitle; - } + base::scoped_nsobject attributedTitle( + [[NSMutableAttributedString alloc] initWithString:title + attributes:attributes]); + if (ANSI_) { + attributedTitle.reset([title attributedStringParsingANSICodes]); + [attributedTitle addAttributes:attributes + range:NSMakeRange(0, [attributedTitle length])]; + return attributedTitle.release(); + } - //NSFontAttributeName:[NSFont menuBarFontOfSize:0], - //NSForegroundColorAttributeName:[self colorWithHighlight: highlight] - - [attributedTitle addAttributes:attributes range:NSMakeRange(0, attributedTitle.length)]; - [attributedTitle addAttribute:NSForegroundColorAttributeName value:[self colorWithHighlight: [self isDarkMode]] range:NSMakeRange(0, attributedTitle.length)]; - - return attributedTitle; + //NSFontAttributeName:[NSFont menuBarFontOfSize:0], + //NSForegroundColorAttributeName:[self colorWithHighlight: highlight] + [attributedTitle addAttributes:attributes + range:NSMakeRange(0, [attributedTitle length])]; + [attributedTitle addAttribute:NSForegroundColorAttributeName + value:[self colorWithHighlight: [self isDarkMode]] + range:NSMakeRange(0, [attributedTitle length])]; + return attributedTitle.release(); } - (void)setMenuController:(AtomMenuController*)menu {