Fixed bug 1641 - avoid allocating nsstring from char*
authorSam Lantinga <slouken@libsdl.org>
Thu, 29 Nov 2012 00:45:36 -0800
changeset 6714146d1d7ecdc4
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
     1.1 --- a/src/video/cocoa/SDL_cocoamessagebox.m	Wed Nov 28 19:19:57 2012 -0800
     1.2 +++ b/src/video/cocoa/SDL_cocoamessagebox.m	Thu Nov 29 00:45:36 2012 -0800
     1.3 @@ -51,8 +51,8 @@
     1.4          [alert setAlertStyle:NSInformationalAlertStyle];
     1.5      }
     1.6  
     1.7 -    [alert setMessageText:[[NSString alloc] initWithUTF8String:messageboxdata->title]];
     1.8 -    [alert setInformativeText:[[NSString alloc] initWithUTF8String:messageboxdata->message]];
     1.9 +    [alert setMessageText:[NSString stringWithUTF8String:messageboxdata->title]];
    1.10 +    [alert setInformativeText:[NSString stringWithUTF8String:messageboxdata->message]];
    1.11  
    1.12      const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
    1.13      int i;
    1.14 @@ -70,6 +70,7 @@
    1.15      NSInteger clicked = [alert runModal];
    1.16      clicked -= NSAlertFirstButtonReturn;
    1.17      *buttonid = buttons[clicked].buttonid;
    1.18 +    [alert release];
    1.19  
    1.20      [pool release];
    1.21  
     2.1 --- a/src/video/cocoa/SDL_cocoavideo.m	Wed Nov 28 19:19:57 2012 -0800
     2.2 +++ b/src/video/cocoa/SDL_cocoavideo.m	Thu Nov 29 00:45:36 2012 -0800
     2.3 @@ -228,11 +228,7 @@
     2.4  
     2.5  void SDL_NSLog(const char *text)
     2.6  {
     2.7 -    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     2.8 -
     2.9 -    NSLog(@"%@", [[NSString alloc] initWithUTF8String:text]);
    2.10 -
    2.11 -    [pool release];
    2.12 +    NSLog(@"%s", text);
    2.13  }
    2.14  
    2.15  /*
     3.1 --- a/src/video/uikit/SDL_uikitmessagebox.m	Wed Nov 28 19:19:57 2012 -0800
     3.2 +++ b/src/video/uikit/SDL_uikitmessagebox.m	Thu Nov 29 00:45:36 2012 -0800
     3.3 @@ -76,8 +76,8 @@
     3.4  
     3.5      UIAlertView* alert = [[UIAlertView alloc] init];
     3.6  
     3.7 -    alert.title = [[NSString alloc] initWithUTF8String:messageboxdata->title];
     3.8 -    alert.message = [[NSString alloc] initWithUTF8String:messageboxdata->message];
     3.9 +    alert.title = [NSString stringWithUTF8String:messageboxdata->title];
    3.10 +    alert.message = [NSString stringWithUTF8String:messageboxdata->message];
    3.11      alert.delegate = [[UIKit_UIAlertViewDelegate alloc] initWithButtonIndex:&clicked];
    3.12  
    3.13      const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
    3.14 @@ -100,6 +100,9 @@
    3.15      s_showingMessageBox = SDL_FALSE;
    3.16  
    3.17      *buttonid = messageboxdata->buttons[clicked].buttonid;
    3.18 + 
    3.19 +    [alert.delegate release];
    3.20 +    [alert release];
    3.21  
    3.22      [pool release];
    3.23  
     4.1 --- a/src/video/uikit/SDL_uikitvideo.m	Wed Nov 28 19:19:57 2012 -0800
     4.2 +++ b/src/video/uikit/SDL_uikitvideo.m	Thu Nov 29 00:45:36 2012 -0800
     4.3 @@ -137,11 +137,7 @@
     4.4  
     4.5  void SDL_NSLog(const char *text)
     4.6  {
     4.7 -    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     4.8 -
     4.9 -    NSLog(@"%@", [[NSString alloc] initWithUTF8String:text]);
    4.10 -
    4.11 -    [pool release];
    4.12 +    NSLog(@"%s", text);
    4.13  }
    4.14  
    4.15  #endif /* SDL_VIDEO_DRIVER_UIKIT */