From 8d45eeb55ca4c74f254f73a5af9a1b0755fa548a Mon Sep 17 00:00:00 2001 From: Ehren Metcalfe Date: Sun, 29 Jan 2017 20:05:55 -0500 Subject: [PATCH] Fix various leaks in Foundation: fixes leak in NSLog and internal logging funcs, NSString and NSMutableString leaks, and an NSTimer leak. Does not include problematic fix for NSThread leak. --- Frameworks/Foundation/NSLog.mm | 3 ++- Frameworks/Foundation/NSLogging.mm | 15 ++++++++++----- Frameworks/Foundation/NSMutableString.mm | 3 ++- Frameworks/Foundation/NSString.mm | 3 ++- Frameworks/Foundation/NSTimer.mm | 2 +- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Frameworks/Foundation/NSLog.mm b/Frameworks/Foundation/NSLog.mm index c610183b83..f589a9037f 100644 --- a/Frameworks/Foundation/NSLog.mm +++ b/Frameworks/Foundation/NSLog.mm @@ -27,7 +27,8 @@ @Status Interoperable */ void NSLogv(NSString* format, va_list list) { - StrongId formattedString = [[NSString alloc] initWithFormat:format arguments:list]; + StrongId formattedString; + formattedString.attach([[NSString alloc] initWithFormat:format arguments:list]); std::wstring wideBuffer = Strings::NarrowToWide(formattedString); // This traces to ETW in debug and release modes. diff --git a/Frameworks/Foundation/NSLogging.mm b/Frameworks/Foundation/NSLogging.mm index 6498e3e2c8..69431ba497 100644 --- a/Frameworks/Foundation/NSLogging.mm +++ b/Frameworks/Foundation/NSLogging.mm @@ -24,7 +24,8 @@ void NSTraceVerbose(const wchar_t* tag, NSString* format, ...) { va_list list; va_start(list, format); - StrongId formattedString = [[NSString alloc] initWithFormat:format arguments:list]; + StrongId formattedString; + formattedString.attach([[NSString alloc] initWithFormat:format arguments:list]); std::wstring wideBuffer = Strings::NarrowToWide(formattedString); TraceVerbose(tag, g_TraceFormat, wideBuffer.c_str()); va_end(list); @@ -33,7 +34,8 @@ void NSTraceVerbose(const wchar_t* tag, NSString* format, ...) { void NSTraceInfo(const wchar_t* tag, NSString* format, ...) { va_list list; va_start(list, format); - StrongId formattedString = [[NSString alloc] initWithFormat:format arguments:list]; + StrongId formattedString; + formattedString.attach([[NSString alloc] initWithFormat:format arguments:list]); std::wstring wideBuffer = Strings::NarrowToWide(formattedString); TraceInfo(tag, g_TraceFormat, wideBuffer.c_str()); va_end(list); @@ -42,7 +44,8 @@ void NSTraceInfo(const wchar_t* tag, NSString* format, ...) { void NSTraceWarning(const wchar_t* tag, NSString* format, ...) { va_list list; va_start(list, format); - StrongId formattedString = [[NSString alloc] initWithFormat:format arguments:list]; + StrongId formattedString; + formattedString.attach([[NSString alloc] initWithFormat:format arguments:list]); std::wstring wideBuffer = Strings::NarrowToWide(formattedString); TraceWarning(tag, g_TraceFormat, wideBuffer.c_str()); va_end(list); @@ -51,7 +54,8 @@ void NSTraceWarning(const wchar_t* tag, NSString* format, ...) { void NSTraceError(const wchar_t* tag, NSString* format, ...) { va_list list; va_start(list, format); - StrongId formattedString = [[NSString alloc] initWithFormat:format arguments:list]; + StrongId formattedString; + formattedString.attach([[NSString alloc] initWithFormat:format arguments:list]); std::wstring wideBuffer = Strings::NarrowToWide(formattedString); TraceError(tag, g_TraceFormat, wideBuffer.c_str()); va_end(list); @@ -60,7 +64,8 @@ void NSTraceError(const wchar_t* tag, NSString* format, ...) { void NSTraceCritical(const wchar_t* tag, NSString* format, ...) { va_list list; va_start(list, format); - StrongId formattedString = [[NSString alloc] initWithFormat:format arguments:list]; + StrongId formattedString; + formattedString.attach([[NSString alloc] initWithFormat:format arguments:list]); std::wstring wideBuffer = Strings::NarrowToWide(formattedString); TraceCritical(tag, g_TraceFormat, wideBuffer.c_str()); va_end(list); diff --git a/Frameworks/Foundation/NSMutableString.mm b/Frameworks/Foundation/NSMutableString.mm index fc53d87e11..52bf266c23 100644 --- a/Frameworks/Foundation/NSMutableString.mm +++ b/Frameworks/Foundation/NSMutableString.mm @@ -97,7 +97,8 @@ - (void)appendFormat:(NSString*)formatStr, ... { va_list reader; va_start(reader, formatStr); - NSString* endStr = [[NSString alloc] initWithFormat:formatStr arguments:reader]; + StrongId endStr; + endStr.attach([[NSString alloc] initWithFormat:formatStr arguments:reader]); va_end(reader); [self appendString:endStr]; diff --git a/Frameworks/Foundation/NSString.mm b/Frameworks/Foundation/NSString.mm index e25b94d488..94f350924d 100644 --- a/Frameworks/Foundation/NSString.mm +++ b/Frameworks/Foundation/NSString.mm @@ -110,7 +110,8 @@ - (NSString*)stringByAppendingFormat:(NSString*)formatStr, ... { va_list reader; va_start(reader, formatStr); - NSString* formattedString = [[NSString alloc] initWithFormat:formatStr arguments:reader]; + StrongId formattedString; + formattedString.attach([[NSString alloc] initWithFormat:formatStr arguments:reader]); return [self stringByAppendingString:formattedString]; } diff --git a/Frameworks/Foundation/NSTimer.mm b/Frameworks/Foundation/NSTimer.mm index ab5fc60536..30a2329fc0 100644 --- a/Frameworks/Foundation/NSTimer.mm +++ b/Frameworks/Foundation/NSTimer.mm @@ -183,7 +183,7 @@ - (instancetype)initWithTimeInterval:(double)seconds _repeats = repeats; _canFire = YES; _valid = YES; - _addedToModes = (NSMutableArray*)CFArrayCreateMutable(NULL, 0, NULL); + _addedToModes.attach((NSMutableArray*)CFArrayCreateMutable(NULL, 0, NULL)); _nextFireTime = [NSDate timeIntervalSinceReferenceDate] + _interval; }