From 7020b3fdc9adaeeb7f5083058109d41bb1fdfdfa Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Thu, 29 Nov 2012 00:45:36 -0800 Subject: [PATCH] Fixed bug 1641 - avoid allocating nsstring from char* Vittorio Giovara 2012-11-12 05:52:47 PST Changesets 4f272256d172 and 42214b6959c5 introduce two neat features for logging and alertbox on ios and osx. However the NSString allocated (and a few other objects) are not freed by the autorelease pool when created by +alloc and -initWithStuff: and this will create leaks. While negligible on osx, on mobile it's better not to have leaks. Attached is a patch that should take care of the problems on both platforms. --- src/video/cocoa/SDL_cocoamessagebox.m | 5 +++-- src/video/cocoa/SDL_cocoavideo.m | 6 +----- src/video/uikit/SDL_uikitmessagebox.m | 7 +++++-- src/video/uikit/SDL_uikitvideo.m | 6 +----- 4 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/video/cocoa/SDL_cocoamessagebox.m b/src/video/cocoa/SDL_cocoamessagebox.m index 7a30d5047..fea487d13 100644 --- a/src/video/cocoa/SDL_cocoamessagebox.m +++ b/src/video/cocoa/SDL_cocoamessagebox.m @@ -51,8 +51,8 @@ [alert setAlertStyle:NSInformationalAlertStyle]; } - [alert setMessageText:[[NSString alloc] initWithUTF8String:messageboxdata->title]]; - [alert setInformativeText:[[NSString alloc] initWithUTF8String:messageboxdata->message]]; + [alert setMessageText:[NSString stringWithUTF8String:messageboxdata->title]]; + [alert setInformativeText:[NSString stringWithUTF8String:messageboxdata->message]]; const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons; int i; @@ -70,6 +70,7 @@ NSInteger clicked = [alert runModal]; clicked -= NSAlertFirstButtonReturn; *buttonid = buttons[clicked].buttonid; + [alert release]; [pool release]; diff --git a/src/video/cocoa/SDL_cocoavideo.m b/src/video/cocoa/SDL_cocoavideo.m index 391cc2fad..aa832bdad 100644 --- a/src/video/cocoa/SDL_cocoavideo.m +++ b/src/video/cocoa/SDL_cocoavideo.m @@ -228,11 +228,7 @@ void SDL_NSLog(const char *text) { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - - NSLog(@"%@", [[NSString alloc] initWithUTF8String:text]); - - [pool release]; + NSLog(@"%s", text); } /* diff --git a/src/video/uikit/SDL_uikitmessagebox.m b/src/video/uikit/SDL_uikitmessagebox.m index 9704b76a8..2ff1d0822 100644 --- a/src/video/uikit/SDL_uikitmessagebox.m +++ b/src/video/uikit/SDL_uikitmessagebox.m @@ -76,8 +76,8 @@ - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger) UIAlertView* alert = [[UIAlertView alloc] init]; - alert.title = [[NSString alloc] initWithUTF8String:messageboxdata->title]; - alert.message = [[NSString alloc] initWithUTF8String:messageboxdata->message]; + alert.title = [NSString stringWithUTF8String:messageboxdata->title]; + alert.message = [NSString stringWithUTF8String:messageboxdata->message]; alert.delegate = [[UIKit_UIAlertViewDelegate alloc] initWithButtonIndex:&clicked]; const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons; @@ -100,6 +100,9 @@ - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger) s_showingMessageBox = SDL_FALSE; *buttonid = messageboxdata->buttons[clicked].buttonid; + + [alert.delegate release]; + [alert release]; [pool release]; diff --git a/src/video/uikit/SDL_uikitvideo.m b/src/video/uikit/SDL_uikitvideo.m index 7de783f34..0c3764e01 100644 --- a/src/video/uikit/SDL_uikitvideo.m +++ b/src/video/uikit/SDL_uikitvideo.m @@ -137,11 +137,7 @@ static void UIKit_DeleteDevice(SDL_VideoDevice * device) void SDL_NSLog(const char *text) { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - - NSLog(@"%@", [[NSString alloc] initWithUTF8String:text]); - - [pool release]; + NSLog(@"%s", text); } #endif /* SDL_VIDEO_DRIVER_UIKIT */