Fixed bug 1641 - avoid allocating nsstring from char*
authorSam Lantinga <slouken@libsdl.org>
Thu, 29 Nov 2012 00:45:36 -0800
changeset 6714 146d1d7ecdc4
parent 6713 fa17a97389ed
child 6715 a02458e71c95
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
src/video/cocoa/SDL_cocoavideo.m
src/video/uikit/SDL_uikitmessagebox.m
src/video/uikit/SDL_uikitvideo.m
--- a/src/video/cocoa/SDL_cocoamessagebox.m	Wed Nov 28 19:19:57 2012 -0800
+++ b/src/video/cocoa/SDL_cocoamessagebox.m	Thu Nov 29 00:45:36 2012 -0800
@@ -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];
 
--- a/src/video/cocoa/SDL_cocoavideo.m	Wed Nov 28 19:19:57 2012 -0800
+++ b/src/video/cocoa/SDL_cocoavideo.m	Thu Nov 29 00:45:36 2012 -0800
@@ -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);
 }
 
 /*
--- a/src/video/uikit/SDL_uikitmessagebox.m	Wed Nov 28 19:19:57 2012 -0800
+++ b/src/video/uikit/SDL_uikitmessagebox.m	Thu Nov 29 00:45:36 2012 -0800
@@ -76,8 +76,8 @@
 
     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 @@
     s_showingMessageBox = SDL_FALSE;
 
     *buttonid = messageboxdata->buttons[clicked].buttonid;
+ 
+    [alert.delegate release];
+    [alert release];
 
     [pool release];
 
--- a/src/video/uikit/SDL_uikitvideo.m	Wed Nov 28 19:19:57 2012 -0800
+++ b/src/video/uikit/SDL_uikitvideo.m	Thu Nov 29 00:45:36 2012 -0800
@@ -137,11 +137,7 @@
 
 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 */