Fixed 2680 - OSX: Replace NSAutoreleasePool with @autoreleasepool

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.
This commit is contained in:
Sam Lantinga 2014-08-17 15:07:00 -07:00
parent 5e50180415
commit d1cc47b337
8 changed files with 34 additions and 78 deletions

View File

@ -33,6 +33,7 @@
Also, note the bundle layouts are different for iPhone and Mac. Also, note the bundle layouts are different for iPhone and Mac.
*/ */
FILE* SDL_OpenFPFromBundleOrFallback(const char *file, const char *mode) FILE* SDL_OpenFPFromBundleOrFallback(const char *file, const char *mode)
{ @autoreleasepool
{ {
FILE* fp = NULL; FILE* fp = NULL;
@ -41,9 +42,6 @@ FILE* SDL_OpenFPFromBundleOrFallback(const char *file, const char *mode)
return fopen(file, mode); return fopen(file, mode);
} }
NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
NSFileManager* file_manager = [NSFileManager defaultManager]; NSFileManager* file_manager = [NSFileManager defaultManager];
NSString* resource_path = [[NSBundle mainBundle] resourcePath]; NSString* resource_path = [[NSBundle mainBundle] resourcePath];
@ -57,10 +55,8 @@ FILE* SDL_OpenFPFromBundleOrFallback(const char *file, const char *mode)
fp = fopen(file, mode); fp = fopen(file, mode);
} }
[autorelease_pool drain];
return fp; return fp;
} }}
#endif /* __MACOSX__ */ #endif /* __MACOSX__ */

View File

@ -35,8 +35,8 @@
char * char *
SDL_GetBasePath(void) SDL_GetBasePath(void)
{ @autoreleasepool
{ {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSBundle *bundle = [NSBundle mainBundle]; NSBundle *bundle = [NSBundle mainBundle];
const char* baseType = [[[bundle infoDictionary] objectForKey:@"SDL_FILESYSTEM_BASE_DIR_TYPE"] UTF8String]; const char* baseType = [[[bundle infoDictionary] objectForKey:@"SDL_FILESYSTEM_BASE_DIR_TYPE"] UTF8String];
const char *base = NULL; const char *base = NULL;
@ -62,14 +62,13 @@ SDL_GetBasePath(void)
} }
} }
[pool release];
return retval; return retval;
} }}
char * char *
SDL_GetPrefPath(const char *org, const char *app) SDL_GetPrefPath(const char *org, const char *app)
{ @autoreleasepool
{ {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
char *retval = NULL; char *retval = NULL;
@ -96,9 +95,8 @@ SDL_GetPrefPath(const char *org, const char *app)
} }
} }
[pool release];
return retval; return retval;
} }}
#endif /* SDL_FILESYSTEM_COCOA */ #endif /* SDL_FILESYSTEM_COCOA */

View File

@ -37,34 +37,28 @@ GetTextFormat(_THIS)
int int
Cocoa_SetClipboardText(_THIS, const char *text) Cocoa_SetClipboardText(_THIS, const char *text)
{ @autoreleasepool
{ {
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
NSAutoreleasePool *pool;
NSPasteboard *pasteboard; NSPasteboard *pasteboard;
NSString *format = GetTextFormat(_this); NSString *format = GetTextFormat(_this);
pool = [[NSAutoreleasePool alloc] init];
pasteboard = [NSPasteboard generalPasteboard]; pasteboard = [NSPasteboard generalPasteboard];
data->clipboard_count = [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil]; data->clipboard_count = [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil];
[pasteboard setString:[NSString stringWithUTF8String:text] forType:format]; [pasteboard setString:[NSString stringWithUTF8String:text] forType:format];
[pool release];
return 0; return 0;
} }}
char * char *
Cocoa_GetClipboardText(_THIS) Cocoa_GetClipboardText(_THIS)
{ @autoreleasepool
{ {
NSAutoreleasePool *pool;
NSPasteboard *pasteboard; NSPasteboard *pasteboard;
NSString *format = GetTextFormat(_this); NSString *format = GetTextFormat(_this);
NSString *available; NSString *available;
char *text; char *text;
pool = [[NSAutoreleasePool alloc] init];
pasteboard = [NSPasteboard generalPasteboard]; pasteboard = [NSPasteboard generalPasteboard];
available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]]; available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
if ([available isEqualToString:format]) { if ([available isEqualToString:format]) {
@ -82,10 +76,8 @@ Cocoa_GetClipboardText(_THIS)
text = SDL_strdup(""); text = SDL_strdup("");
} }
[pool release];
return text; return text;
} }}
SDL_bool SDL_bool
Cocoa_HasClipboardText(_THIS) Cocoa_HasClipboardText(_THIS)
@ -101,13 +93,11 @@ Cocoa_HasClipboardText(_THIS)
void void
Cocoa_CheckClipboardUpdate(struct SDL_VideoData * data) Cocoa_CheckClipboardUpdate(struct SDL_VideoData * data)
{ @autoreleasepool
{ {
NSAutoreleasePool *pool;
NSPasteboard *pasteboard; NSPasteboard *pasteboard;
NSInteger count; NSInteger count;
pool = [[NSAutoreleasePool alloc] init];
pasteboard = [NSPasteboard generalPasteboard]; pasteboard = [NSPasteboard generalPasteboard];
count = [pasteboard changeCount]; count = [pasteboard changeCount];
if (count != data->clipboard_count) { if (count != data->clipboard_count) {
@ -116,9 +106,7 @@ Cocoa_CheckClipboardUpdate(struct SDL_VideoData * data)
} }
data->clipboard_count = count; data->clipboard_count = count;
} }
}}
[pool release];
}
#endif /* SDL_VIDEO_DRIVER_COCOA */ #endif /* SDL_VIDEO_DRIVER_COCOA */

View File

@ -248,17 +248,16 @@ CreateApplicationMenus(void)
void void
Cocoa_RegisterApp(void) Cocoa_RegisterApp(void)
{ @autoreleasepool
{ {
/* This can get called more than once! Be careful what you initialize! */ /* This can get called more than once! Be careful what you initialize! */
ProcessSerialNumber psn; ProcessSerialNumber psn;
NSAutoreleasePool *pool;
if (!GetCurrentProcess(&psn)) { if (!GetCurrentProcess(&psn)) {
TransformProcessType(&psn, kProcessTransformToForegroundApplication); TransformProcessType(&psn, kProcessTransformToForegroundApplication);
SetFrontProcess(&psn); SetFrontProcess(&psn);
} }
pool = [[NSAutoreleasePool alloc] init];
if (NSApp == nil) { if (NSApp == nil) {
[SDLApplication sharedApplication]; [SDLApplication sharedApplication];
SDL_assert(NSApp != nil); SDL_assert(NSApp != nil);
@ -287,14 +286,12 @@ Cocoa_RegisterApp(void)
appDelegate->seenFirstActivate = YES; appDelegate->seenFirstActivate = YES;
} }
} }
[pool release]; }}
}
void void
Cocoa_PumpEvents(_THIS) Cocoa_PumpEvents(_THIS)
{ @autoreleasepool
{ {
NSAutoreleasePool *pool;
/* Update activity every 30 seconds to prevent screensaver */ /* Update activity every 30 seconds to prevent screensaver */
if (_this->suspend_screensaver) { if (_this->suspend_screensaver) {
SDL_VideoData *data = (SDL_VideoData *)_this->driverdata; SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
@ -306,7 +303,6 @@ Cocoa_PumpEvents(_THIS)
} }
} }
pool = [[NSAutoreleasePool alloc] init];
for ( ; ; ) { for ( ; ; ) {
NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ]; NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
if ( event == nil ) { if ( event == nil ) {
@ -338,8 +334,7 @@ Cocoa_PumpEvents(_THIS)
/* Pass through to NSApp to make sure everything stays in sync */ /* Pass through to NSApp to make sure everything stays in sync */
[NSApp sendEvent:event]; [NSApp sendEvent:event];
} }
[pool release]; }}
}
#endif /* SDL_VIDEO_DRIVER_COCOA */ #endif /* SDL_VIDEO_DRIVER_COCOA */

View File

@ -479,9 +479,9 @@ Cocoa_InitKeyboard(_THIS)
void void
Cocoa_StartTextInput(_THIS) Cocoa_StartTextInput(_THIS)
{ @autoreleasepool
{ {
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
SDL_Window *window = SDL_GetKeyboardFocus(); SDL_Window *window = SDL_GetKeyboardFocus();
NSWindow *nswindow = nil; NSWindow *nswindow = nil;
if (window) { if (window) {
@ -506,23 +506,20 @@ Cocoa_StartTextInput(_THIS)
[parentView addSubview: data->fieldEdit]; [parentView addSubview: data->fieldEdit];
[nswindow makeFirstResponder: data->fieldEdit]; [nswindow makeFirstResponder: data->fieldEdit];
} }
}}
[pool release];
}
void void
Cocoa_StopTextInput(_THIS) Cocoa_StopTextInput(_THIS)
{ @autoreleasepool
{ {
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
if (data && data->fieldEdit) { if (data && data->fieldEdit) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[data->fieldEdit removeFromSuperview]; [data->fieldEdit removeFromSuperview];
[data->fieldEdit release]; [data->fieldEdit release];
data->fieldEdit = nil; data->fieldEdit = nil;
[pool release];
}
} }
}}
void void
Cocoa_SetTextInputRect(_THIS, SDL_Rect *rect) Cocoa_SetTextInputRect(_THIS, SDL_Rect *rect)

View File

@ -79,11 +79,10 @@
/* Display a Cocoa message box */ /* Display a Cocoa message box */
int int
Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid) Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
{ @autoreleasepool
{ {
Cocoa_RegisterApp(); Cocoa_RegisterApp();
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSAlert* alert = [[[NSAlert alloc] init] autorelease]; NSAlert* alert = [[[NSAlert alloc] init] autorelease];
if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) { if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) {
@ -125,10 +124,8 @@ Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
returnValue = SDL_SetError("Did not get a valid `clicked button' id: %ld", (long)clicked); returnValue = SDL_SetError("Did not get a valid `clicked button' id: %ld", (long)clicked);
} }
[pool release];
return returnValue; return returnValue;
} }}
#endif /* SDL_VIDEO_DRIVER_COCOA */ #endif /* SDL_VIDEO_DRIVER_COCOA */

View File

@ -214,8 +214,8 @@ Cocoa_GetDisplayName(CGDirectDisplayID displayID)
void void
Cocoa_InitModes(_THIS) Cocoa_InitModes(_THIS)
{ @autoreleasepool
{ {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CGDisplayErr result; CGDisplayErr result;
CGDirectDisplayID *displays; CGDirectDisplayID *displays;
CGDisplayCount numDisplays; CGDisplayCount numDisplays;
@ -224,7 +224,6 @@ Cocoa_InitModes(_THIS)
result = CGGetOnlineDisplayList(0, NULL, &numDisplays); result = CGGetOnlineDisplayList(0, NULL, &numDisplays);
if (result != kCGErrorSuccess) { if (result != kCGErrorSuccess) {
CG_SetError("CGGetOnlineDisplayList()", result); CG_SetError("CGGetOnlineDisplayList()", result);
[pool release];
return; return;
} }
displays = SDL_stack_alloc(CGDirectDisplayID, numDisplays); displays = SDL_stack_alloc(CGDirectDisplayID, numDisplays);
@ -232,7 +231,6 @@ Cocoa_InitModes(_THIS)
if (result != kCGErrorSuccess) { if (result != kCGErrorSuccess) {
CG_SetError("CGGetOnlineDisplayList()", result); CG_SetError("CGGetOnlineDisplayList()", result);
SDL_stack_free(displays); SDL_stack_free(displays);
[pool release];
return; return;
} }
@ -297,8 +295,7 @@ Cocoa_InitModes(_THIS)
} }
} }
SDL_stack_free(displays); SDL_stack_free(displays);
[pool release]; }}
}
int int
Cocoa_GetDisplayBounds(_THIS, SDL_VideoDisplay * display, SDL_Rect * rect) Cocoa_GetDisplayBounds(_THIS, SDL_VideoDisplay * display, SDL_Rect * rect)

View File

@ -66,8 +66,8 @@
static SDL_Cursor * static SDL_Cursor *
Cocoa_CreateDefaultCursor() Cocoa_CreateDefaultCursor()
{ @autoreleasepool
{ {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSCursor *nscursor; NSCursor *nscursor;
SDL_Cursor *cursor = NULL; SDL_Cursor *cursor = NULL;
@ -81,15 +81,13 @@ Cocoa_CreateDefaultCursor()
} }
} }
[pool release];
return cursor; return cursor;
} }}
static SDL_Cursor * static SDL_Cursor *
Cocoa_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y) Cocoa_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
{ @autoreleasepool
{ {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSImage *nsimage; NSImage *nsimage;
NSCursor *nscursor = NULL; NSCursor *nscursor = NULL;
SDL_Cursor *cursor = NULL; SDL_Cursor *cursor = NULL;
@ -108,15 +106,13 @@ Cocoa_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
} }
} }
[pool release];
return cursor; return cursor;
} }}
static SDL_Cursor * static SDL_Cursor *
Cocoa_CreateSystemCursor(SDL_SystemCursor id) Cocoa_CreateSystemCursor(SDL_SystemCursor id)
{ @autoreleasepool
{ {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSCursor *nscursor = NULL; NSCursor *nscursor = NULL;
SDL_Cursor *cursor = NULL; SDL_Cursor *cursor = NULL;
@ -169,28 +165,23 @@ Cocoa_CreateSystemCursor(SDL_SystemCursor id)
} }
} }
[pool release];
return cursor; return cursor;
} }}
static void static void
Cocoa_FreeCursor(SDL_Cursor * cursor) Cocoa_FreeCursor(SDL_Cursor * cursor)
{ @autoreleasepool
{ {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSCursor *nscursor = (NSCursor *)cursor->driverdata; NSCursor *nscursor = (NSCursor *)cursor->driverdata;
[nscursor release]; [nscursor release];
SDL_free(cursor); SDL_free(cursor);
}}
[pool release];
}
static int static int
Cocoa_ShowCursor(SDL_Cursor * cursor) Cocoa_ShowCursor(SDL_Cursor * cursor)
{ @autoreleasepool
{ {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
SDL_VideoDevice *device = SDL_GetVideoDevice(); SDL_VideoDevice *device = SDL_GetVideoDevice();
SDL_Window *window = (device ? device->windows : NULL); SDL_Window *window = (device ? device->windows : NULL);
for (; window != NULL; window = window->next) { for (; window != NULL; window = window->next) {
@ -201,11 +192,8 @@ Cocoa_ShowCursor(SDL_Cursor * cursor)
waitUntilDone:NO]; waitUntilDone:NO];
} }
} }
[pool release];
return 0; return 0;
} }}
static void static void
Cocoa_WarpMouseGlobal(int x, int y) Cocoa_WarpMouseGlobal(int x, int y)