Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Fixed bug 1641 - avoid allocating nsstring from char*
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
slouken committed Nov 29, 2012
1 parent 4a744e7 commit 7020b3f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/video/cocoa/SDL_cocoamessagebox.m
Expand Up @@ -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;
Expand All @@ -70,6 +70,7 @@
NSInteger clicked = [alert runModal];
clicked -= NSAlertFirstButtonReturn;
*buttonid = buttons[clicked].buttonid;
[alert release];

[pool release];

Expand Down
6 changes: 1 addition & 5 deletions src/video/cocoa/SDL_cocoavideo.m
Expand Up @@ -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);
}

/*
Expand Down
7 changes: 5 additions & 2 deletions src/video/uikit/SDL_uikitmessagebox.m
Expand Up @@ -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;
Expand All @@ -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];

Expand Down
6 changes: 1 addition & 5 deletions src/video/uikit/SDL_uikitvideo.m
Expand Up @@ -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 */
Expand Down

0 comments on commit 7020b3f

Please sign in to comment.