Skip to content

Commit

Permalink
Fixed 2680 - OSX: Replace NSAutoreleasePool with @autoreleasepool
Browse files Browse the repository at this point in the history
Tim McDaniel

This patch replaces all use of NSAutoreleasePool with the Apple recommended @autoreleasepool.  @autoreleasepool is supposedly more efficient, and since it is scope based it can't be accidentally not released.
  • Loading branch information
slouken committed Aug 17, 2014
1 parent 5e50180 commit d1cc47b
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 78 deletions.
8 changes: 2 additions & 6 deletions src/file/cocoa/SDL_rwopsbundlesupport.m
Expand Up @@ -33,6 +33,7 @@
Also, note the bundle layouts are different for iPhone and Mac.
*/
FILE* SDL_OpenFPFromBundleOrFallback(const char *file, const char *mode)
{ @autoreleasepool
{
FILE* fp = NULL;

Expand All @@ -41,9 +42,6 @@
return fopen(file, mode);
}

NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];


NSFileManager* file_manager = [NSFileManager defaultManager];
NSString* resource_path = [[NSBundle mainBundle] resourcePath];

Expand All @@ -57,10 +55,8 @@
fp = fopen(file, mode);
}

[autorelease_pool drain];

return fp;
}
}}

#endif /* __MACOSX__ */

Expand Down
10 changes: 4 additions & 6 deletions src/filesystem/cocoa/SDL_sysfilesystem.m
Expand Up @@ -35,8 +35,8 @@

char *
SDL_GetBasePath(void)
{ @autoreleasepool
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSBundle *bundle = [NSBundle mainBundle];
const char* baseType = [[[bundle infoDictionary] objectForKey:@"SDL_FILESYSTEM_BASE_DIR_TYPE"] UTF8String];
const char *base = NULL;
Expand All @@ -62,14 +62,13 @@
}
}

[pool release];
return retval;
}
}}

char *
SDL_GetPrefPath(const char *org, const char *app)
{ @autoreleasepool
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
char *retval = NULL;

Expand All @@ -96,9 +95,8 @@
}
}

[pool release];
return retval;
}
}}

#endif /* SDL_FILESYSTEM_COCOA */

Expand Down
24 changes: 6 additions & 18 deletions src/video/cocoa/SDL_cocoaclipboard.m
Expand Up @@ -37,34 +37,28 @@

int
Cocoa_SetClipboardText(_THIS, const char *text)
{ @autoreleasepool
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
NSAutoreleasePool *pool;
NSPasteboard *pasteboard;
NSString *format = GetTextFormat(_this);

pool = [[NSAutoreleasePool alloc] init];

pasteboard = [NSPasteboard generalPasteboard];
data->clipboard_count = [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil];
[pasteboard setString:[NSString stringWithUTF8String:text] forType:format];

[pool release];

return 0;
}
}}

char *
Cocoa_GetClipboardText(_THIS)
{ @autoreleasepool
{
NSAutoreleasePool *pool;
NSPasteboard *pasteboard;
NSString *format = GetTextFormat(_this);
NSString *available;
char *text;

pool = [[NSAutoreleasePool alloc] init];

pasteboard = [NSPasteboard generalPasteboard];
available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
if ([available isEqualToString:format]) {
Expand All @@ -82,10 +76,8 @@
text = SDL_strdup("");
}

[pool release];

return text;
}
}}

SDL_bool
Cocoa_HasClipboardText(_THIS)
Expand All @@ -101,13 +93,11 @@

void
Cocoa_CheckClipboardUpdate(struct SDL_VideoData * data)
{ @autoreleasepool
{
NSAutoreleasePool *pool;
NSPasteboard *pasteboard;
NSInteger count;

pool = [[NSAutoreleasePool alloc] init];

pasteboard = [NSPasteboard generalPasteboard];
count = [pasteboard changeCount];
if (count != data->clipboard_count) {
Expand All @@ -116,9 +106,7 @@
}
data->clipboard_count = count;
}

[pool release];
}
}}

#endif /* SDL_VIDEO_DRIVER_COCOA */

Expand Down
13 changes: 4 additions & 9 deletions src/video/cocoa/SDL_cocoaevents.m
Expand Up @@ -248,17 +248,16 @@ - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filenam

void
Cocoa_RegisterApp(void)
{ @autoreleasepool
{
/* This can get called more than once! Be careful what you initialize! */
ProcessSerialNumber psn;
NSAutoreleasePool *pool;

if (!GetCurrentProcess(&psn)) {
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
SetFrontProcess(&psn);
}

pool = [[NSAutoreleasePool alloc] init];
if (NSApp == nil) {
[SDLApplication sharedApplication];
SDL_assert(NSApp != nil);
Expand Down Expand Up @@ -287,14 +286,12 @@ - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filenam
appDelegate->seenFirstActivate = YES;
}
}
[pool release];
}
}}

void
Cocoa_PumpEvents(_THIS)
{ @autoreleasepool
{
NSAutoreleasePool *pool;

/* Update activity every 30 seconds to prevent screensaver */
if (_this->suspend_screensaver) {
SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
Expand All @@ -306,7 +303,6 @@ - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filenam
}
}

pool = [[NSAutoreleasePool alloc] init];
for ( ; ; ) {
NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
if ( event == nil ) {
Expand Down Expand Up @@ -338,8 +334,7 @@ - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filenam
/* Pass through to NSApp to make sure everything stays in sync */
[NSApp sendEvent:event];
}
[pool release];
}
}}

#endif /* SDL_VIDEO_DRIVER_COCOA */

Expand Down
11 changes: 4 additions & 7 deletions src/video/cocoa/SDL_cocoakeyboard.m
Expand Up @@ -479,9 +479,9 @@ - (NSArray *) validAttributesForMarkedText

void
Cocoa_StartTextInput(_THIS)
{ @autoreleasepool
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
SDL_Window *window = SDL_GetKeyboardFocus();
NSWindow *nswindow = nil;
if (window) {
Expand All @@ -506,23 +506,20 @@ - (NSArray *) validAttributesForMarkedText
[parentView addSubview: data->fieldEdit];
[nswindow makeFirstResponder: data->fieldEdit];
}

[pool release];
}
}}

void
Cocoa_StopTextInput(_THIS)
{ @autoreleasepool
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;

if (data && data->fieldEdit) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[data->fieldEdit removeFromSuperview];
[data->fieldEdit release];
data->fieldEdit = nil;
[pool release];
}
}
}}

void
Cocoa_SetTextInputRect(_THIS, SDL_Rect *rect)
Expand Down
7 changes: 2 additions & 5 deletions src/video/cocoa/SDL_cocoamessagebox.m
Expand Up @@ -79,11 +79,10 @@ - (void) alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextIn
/* Display a Cocoa message box */
int
Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
{ @autoreleasepool
{
Cocoa_RegisterApp();

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSAlert* alert = [[[NSAlert alloc] init] autorelease];

if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) {
Expand Down Expand Up @@ -125,10 +124,8 @@ - (void) alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextIn
returnValue = SDL_SetError("Did not get a valid `clicked button' id: %ld", (long)clicked);
}

[pool release];

return returnValue;
}
}}

#endif /* SDL_VIDEO_DRIVER_COCOA */

Expand Down
7 changes: 2 additions & 5 deletions src/video/cocoa/SDL_cocoamodes.m
Expand Up @@ -214,8 +214,8 @@

void
Cocoa_InitModes(_THIS)
{ @autoreleasepool
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CGDisplayErr result;
CGDirectDisplayID *displays;
CGDisplayCount numDisplays;
Expand All @@ -224,15 +224,13 @@
result = CGGetOnlineDisplayList(0, NULL, &numDisplays);
if (result != kCGErrorSuccess) {
CG_SetError("CGGetOnlineDisplayList()", result);
[pool release];
return;
}
displays = SDL_stack_alloc(CGDirectDisplayID, numDisplays);
result = CGGetOnlineDisplayList(numDisplays, displays, &numDisplays);
if (result != kCGErrorSuccess) {
CG_SetError("CGGetOnlineDisplayList()", result);
SDL_stack_free(displays);
[pool release];
return;
}

Expand Down Expand Up @@ -297,8 +295,7 @@
}
}
SDL_stack_free(displays);
[pool release];
}
}}

int
Cocoa_GetDisplayBounds(_THIS, SDL_VideoDisplay * display, SDL_Rect * rect)
Expand Down
32 changes: 10 additions & 22 deletions src/video/cocoa/SDL_cocoamouse.m
Expand Up @@ -66,8 +66,8 @@ + (NSCursor *)invisibleCursor

static SDL_Cursor *
Cocoa_CreateDefaultCursor()
{ @autoreleasepool
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSCursor *nscursor;
SDL_Cursor *cursor = NULL;

Expand All @@ -81,15 +81,13 @@ + (NSCursor *)invisibleCursor
}
}

[pool release];

return cursor;
}
}}

static SDL_Cursor *
Cocoa_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
{ @autoreleasepool
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSImage *nsimage;
NSCursor *nscursor = NULL;
SDL_Cursor *cursor = NULL;
Expand All @@ -108,15 +106,13 @@ + (NSCursor *)invisibleCursor
}
}

[pool release];

return cursor;
}
}}

static SDL_Cursor *
Cocoa_CreateSystemCursor(SDL_SystemCursor id)
{ @autoreleasepool
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSCursor *nscursor = NULL;
SDL_Cursor *cursor = NULL;

Expand Down Expand Up @@ -169,28 +165,23 @@ + (NSCursor *)invisibleCursor
}
}

[pool release];

return cursor;
}
}}

static void
Cocoa_FreeCursor(SDL_Cursor * cursor)
{ @autoreleasepool
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSCursor *nscursor = (NSCursor *)cursor->driverdata;

[nscursor release];
SDL_free(cursor);

[pool release];
}
}}

static int
Cocoa_ShowCursor(SDL_Cursor * cursor)
{ @autoreleasepool
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

SDL_VideoDevice *device = SDL_GetVideoDevice();
SDL_Window *window = (device ? device->windows : NULL);
for (; window != NULL; window = window->next) {
Expand All @@ -201,11 +192,8 @@ + (NSCursor *)invisibleCursor
waitUntilDone:NO];
}
}

[pool release];

return 0;
}
}}

static void
Cocoa_WarpMouseGlobal(int x, int y)
Expand Down

0 comments on commit d1cc47b

Please sign in to comment.