Fixed 2680 - OSX: Replace NSAutoreleasePool with @autoreleasepool
authorSam Lantinga <slouken@libsdl.org>
Sun, 17 Aug 2014 15:07:00 -0700
changeset 9087eef2e43f60d1
parent 9086 c5e33f9a0d03
child 9088 c94127dcbe97
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.
src/file/cocoa/SDL_rwopsbundlesupport.m
src/filesystem/cocoa/SDL_sysfilesystem.m
src/video/cocoa/SDL_cocoaclipboard.m
src/video/cocoa/SDL_cocoaevents.m
src/video/cocoa/SDL_cocoakeyboard.m
src/video/cocoa/SDL_cocoamessagebox.m
src/video/cocoa/SDL_cocoamodes.m
src/video/cocoa/SDL_cocoamouse.m
     1.1 --- a/src/file/cocoa/SDL_rwopsbundlesupport.m	Sun Aug 17 14:57:52 2014 -0700
     1.2 +++ b/src/file/cocoa/SDL_rwopsbundlesupport.m	Sun Aug 17 15:07:00 2014 -0700
     1.3 @@ -33,6 +33,7 @@
     1.4   Also, note the bundle layouts are different for iPhone and Mac.
     1.5  */
     1.6  FILE* SDL_OpenFPFromBundleOrFallback(const char *file, const char *mode)
     1.7 +{ @autoreleasepool
     1.8  {
     1.9      FILE* fp = NULL;
    1.10  
    1.11 @@ -41,9 +42,6 @@
    1.12          return fopen(file, mode);
    1.13      }
    1.14  
    1.15 -    NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
    1.16 -
    1.17 -
    1.18      NSFileManager* file_manager = [NSFileManager defaultManager];
    1.19      NSString* resource_path = [[NSBundle mainBundle] resourcePath];
    1.20  
    1.21 @@ -57,10 +55,8 @@
    1.22          fp = fopen(file, mode);
    1.23      }
    1.24  
    1.25 -    [autorelease_pool drain];
    1.26 -
    1.27      return fp;
    1.28 -}
    1.29 +}}
    1.30  
    1.31  #endif /* __MACOSX__ */
    1.32  
     2.1 --- a/src/filesystem/cocoa/SDL_sysfilesystem.m	Sun Aug 17 14:57:52 2014 -0700
     2.2 +++ b/src/filesystem/cocoa/SDL_sysfilesystem.m	Sun Aug 17 15:07:00 2014 -0700
     2.3 @@ -35,8 +35,8 @@
     2.4  
     2.5  char *
     2.6  SDL_GetBasePath(void)
     2.7 +{ @autoreleasepool
     2.8  {
     2.9 -    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    2.10      NSBundle *bundle = [NSBundle mainBundle];
    2.11      const char* baseType = [[[bundle infoDictionary] objectForKey:@"SDL_FILESYSTEM_BASE_DIR_TYPE"] UTF8String];
    2.12      const char *base = NULL;
    2.13 @@ -62,14 +62,13 @@
    2.14          }
    2.15      }
    2.16  
    2.17 -    [pool release];
    2.18      return retval;
    2.19 -}
    2.20 +}}
    2.21  
    2.22  char *
    2.23  SDL_GetPrefPath(const char *org, const char *app)
    2.24 +{ @autoreleasepool
    2.25  {
    2.26 -    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    2.27      NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
    2.28      char *retval = NULL;
    2.29  
    2.30 @@ -96,9 +95,8 @@
    2.31          }
    2.32      }
    2.33  
    2.34 -    [pool release];
    2.35      return retval;
    2.36 -}
    2.37 +}}
    2.38  
    2.39  #endif /* SDL_FILESYSTEM_COCOA */
    2.40  
     3.1 --- a/src/video/cocoa/SDL_cocoaclipboard.m	Sun Aug 17 14:57:52 2014 -0700
     3.2 +++ b/src/video/cocoa/SDL_cocoaclipboard.m	Sun Aug 17 15:07:00 2014 -0700
     3.3 @@ -37,34 +37,28 @@
     3.4  
     3.5  int
     3.6  Cocoa_SetClipboardText(_THIS, const char *text)
     3.7 +{ @autoreleasepool
     3.8  {
     3.9      SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    3.10 -    NSAutoreleasePool *pool;
    3.11      NSPasteboard *pasteboard;
    3.12      NSString *format = GetTextFormat(_this);
    3.13  
    3.14 -    pool = [[NSAutoreleasePool alloc] init];
    3.15 -
    3.16      pasteboard = [NSPasteboard generalPasteboard];
    3.17      data->clipboard_count = [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil];
    3.18      [pasteboard setString:[NSString stringWithUTF8String:text] forType:format];
    3.19  
    3.20 -    [pool release];
    3.21 -
    3.22      return 0;
    3.23 -}
    3.24 +}}
    3.25  
    3.26  char *
    3.27  Cocoa_GetClipboardText(_THIS)
    3.28 +{ @autoreleasepool
    3.29  {
    3.30 -    NSAutoreleasePool *pool;
    3.31      NSPasteboard *pasteboard;
    3.32      NSString *format = GetTextFormat(_this);
    3.33      NSString *available;
    3.34      char *text;
    3.35  
    3.36 -    pool = [[NSAutoreleasePool alloc] init];
    3.37 -
    3.38      pasteboard = [NSPasteboard generalPasteboard];
    3.39      available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
    3.40      if ([available isEqualToString:format]) {
    3.41 @@ -82,10 +76,8 @@
    3.42          text = SDL_strdup("");
    3.43      }
    3.44  
    3.45 -    [pool release];
    3.46 -
    3.47      return text;
    3.48 -}
    3.49 +}}
    3.50  
    3.51  SDL_bool
    3.52  Cocoa_HasClipboardText(_THIS)
    3.53 @@ -101,13 +93,11 @@
    3.54  
    3.55  void
    3.56  Cocoa_CheckClipboardUpdate(struct SDL_VideoData * data)
    3.57 +{ @autoreleasepool
    3.58  {
    3.59 -    NSAutoreleasePool *pool;
    3.60      NSPasteboard *pasteboard;
    3.61      NSInteger count;
    3.62  
    3.63 -    pool = [[NSAutoreleasePool alloc] init];
    3.64 -
    3.65      pasteboard = [NSPasteboard generalPasteboard];
    3.66      count = [pasteboard changeCount];
    3.67      if (count != data->clipboard_count) {
    3.68 @@ -116,9 +106,7 @@
    3.69          }
    3.70          data->clipboard_count = count;
    3.71      }
    3.72 -
    3.73 -    [pool release];
    3.74 -}
    3.75 +}}
    3.76  
    3.77  #endif /* SDL_VIDEO_DRIVER_COCOA */
    3.78  
     4.1 --- a/src/video/cocoa/SDL_cocoaevents.m	Sun Aug 17 14:57:52 2014 -0700
     4.2 +++ b/src/video/cocoa/SDL_cocoaevents.m	Sun Aug 17 15:07:00 2014 -0700
     4.3 @@ -248,17 +248,16 @@
     4.4  
     4.5  void
     4.6  Cocoa_RegisterApp(void)
     4.7 +{ @autoreleasepool
     4.8  {
     4.9      /* This can get called more than once! Be careful what you initialize! */
    4.10      ProcessSerialNumber psn;
    4.11 -    NSAutoreleasePool *pool;
    4.12  
    4.13      if (!GetCurrentProcess(&psn)) {
    4.14          TransformProcessType(&psn, kProcessTransformToForegroundApplication);
    4.15          SetFrontProcess(&psn);
    4.16      }
    4.17  
    4.18 -    pool = [[NSAutoreleasePool alloc] init];
    4.19      if (NSApp == nil) {
    4.20          [SDLApplication sharedApplication];
    4.21          SDL_assert(NSApp != nil);
    4.22 @@ -287,14 +286,12 @@
    4.23              appDelegate->seenFirstActivate = YES;
    4.24          }
    4.25      }
    4.26 -    [pool release];
    4.27 -}
    4.28 +}}
    4.29  
    4.30  void
    4.31  Cocoa_PumpEvents(_THIS)
    4.32 +{ @autoreleasepool
    4.33  {
    4.34 -    NSAutoreleasePool *pool;
    4.35 -
    4.36      /* Update activity every 30 seconds to prevent screensaver */
    4.37      if (_this->suspend_screensaver) {
    4.38          SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
    4.39 @@ -306,7 +303,6 @@
    4.40          }
    4.41      }
    4.42  
    4.43 -    pool = [[NSAutoreleasePool alloc] init];
    4.44      for ( ; ; ) {
    4.45          NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
    4.46          if ( event == nil ) {
    4.47 @@ -338,8 +334,7 @@
    4.48          /* Pass through to NSApp to make sure everything stays in sync */
    4.49          [NSApp sendEvent:event];
    4.50      }
    4.51 -    [pool release];
    4.52 -}
    4.53 +}}
    4.54  
    4.55  #endif /* SDL_VIDEO_DRIVER_COCOA */
    4.56  
     5.1 --- a/src/video/cocoa/SDL_cocoakeyboard.m	Sun Aug 17 14:57:52 2014 -0700
     5.2 +++ b/src/video/cocoa/SDL_cocoakeyboard.m	Sun Aug 17 15:07:00 2014 -0700
     5.3 @@ -479,9 +479,9 @@
     5.4  
     5.5  void
     5.6  Cocoa_StartTextInput(_THIS)
     5.7 +{ @autoreleasepool
     5.8  {
     5.9      SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    5.10 -    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    5.11      SDL_Window *window = SDL_GetKeyboardFocus();
    5.12      NSWindow *nswindow = nil;
    5.13      if (window) {
    5.14 @@ -506,23 +506,20 @@
    5.15          [parentView addSubview: data->fieldEdit];
    5.16          [nswindow makeFirstResponder: data->fieldEdit];
    5.17      }
    5.18 -
    5.19 -    [pool release];
    5.20 -}
    5.21 +}}
    5.22  
    5.23  void
    5.24  Cocoa_StopTextInput(_THIS)
    5.25 +{ @autoreleasepool
    5.26  {
    5.27      SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    5.28  
    5.29      if (data && data->fieldEdit) {
    5.30 -        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    5.31          [data->fieldEdit removeFromSuperview];
    5.32          [data->fieldEdit release];
    5.33          data->fieldEdit = nil;
    5.34 -        [pool release];
    5.35      }
    5.36 -}
    5.37 +}}
    5.38  
    5.39  void
    5.40  Cocoa_SetTextInputRect(_THIS, SDL_Rect *rect)
     6.1 --- a/src/video/cocoa/SDL_cocoamessagebox.m	Sun Aug 17 14:57:52 2014 -0700
     6.2 +++ b/src/video/cocoa/SDL_cocoamessagebox.m	Sun Aug 17 15:07:00 2014 -0700
     6.3 @@ -79,11 +79,10 @@
     6.4  /* Display a Cocoa message box */
     6.5  int
     6.6  Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
     6.7 +{ @autoreleasepool
     6.8  {
     6.9      Cocoa_RegisterApp();
    6.10  
    6.11 -    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    6.12 -
    6.13      NSAlert* alert = [[[NSAlert alloc] init] autorelease];
    6.14  
    6.15      if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) {
    6.16 @@ -125,10 +124,8 @@
    6.17          returnValue = SDL_SetError("Did not get a valid `clicked button' id: %ld", (long)clicked);
    6.18      }
    6.19  
    6.20 -    [pool release];
    6.21 -
    6.22      return returnValue;
    6.23 -}
    6.24 +}}
    6.25  
    6.26  #endif /* SDL_VIDEO_DRIVER_COCOA */
    6.27  
     7.1 --- a/src/video/cocoa/SDL_cocoamodes.m	Sun Aug 17 14:57:52 2014 -0700
     7.2 +++ b/src/video/cocoa/SDL_cocoamodes.m	Sun Aug 17 15:07:00 2014 -0700
     7.3 @@ -214,8 +214,8 @@
     7.4  
     7.5  void
     7.6  Cocoa_InitModes(_THIS)
     7.7 +{ @autoreleasepool
     7.8  {
     7.9 -    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    7.10      CGDisplayErr result;
    7.11      CGDirectDisplayID *displays;
    7.12      CGDisplayCount numDisplays;
    7.13 @@ -224,7 +224,6 @@
    7.14      result = CGGetOnlineDisplayList(0, NULL, &numDisplays);
    7.15      if (result != kCGErrorSuccess) {
    7.16          CG_SetError("CGGetOnlineDisplayList()", result);
    7.17 -        [pool release];
    7.18          return;
    7.19      }
    7.20      displays = SDL_stack_alloc(CGDirectDisplayID, numDisplays);
    7.21 @@ -232,7 +231,6 @@
    7.22      if (result != kCGErrorSuccess) {
    7.23          CG_SetError("CGGetOnlineDisplayList()", result);
    7.24          SDL_stack_free(displays);
    7.25 -        [pool release];
    7.26          return;
    7.27      }
    7.28  
    7.29 @@ -297,8 +295,7 @@
    7.30          }
    7.31      }
    7.32      SDL_stack_free(displays);
    7.33 -    [pool release];
    7.34 -}
    7.35 +}}
    7.36  
    7.37  int
    7.38  Cocoa_GetDisplayBounds(_THIS, SDL_VideoDisplay * display, SDL_Rect * rect)
     8.1 --- a/src/video/cocoa/SDL_cocoamouse.m	Sun Aug 17 14:57:52 2014 -0700
     8.2 +++ b/src/video/cocoa/SDL_cocoamouse.m	Sun Aug 17 15:07:00 2014 -0700
     8.3 @@ -66,8 +66,8 @@
     8.4  
     8.5  static SDL_Cursor *
     8.6  Cocoa_CreateDefaultCursor()
     8.7 +{ @autoreleasepool
     8.8  {
     8.9 -    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    8.10      NSCursor *nscursor;
    8.11      SDL_Cursor *cursor = NULL;
    8.12  
    8.13 @@ -81,15 +81,13 @@
    8.14          }
    8.15      }
    8.16  
    8.17 -    [pool release];
    8.18 -
    8.19      return cursor;
    8.20 -}
    8.21 +}}
    8.22  
    8.23  static SDL_Cursor *
    8.24  Cocoa_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
    8.25 +{ @autoreleasepool
    8.26  {
    8.27 -    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    8.28      NSImage *nsimage;
    8.29      NSCursor *nscursor = NULL;
    8.30      SDL_Cursor *cursor = NULL;
    8.31 @@ -108,15 +106,13 @@
    8.32          }
    8.33      }
    8.34  
    8.35 -    [pool release];
    8.36 -
    8.37      return cursor;
    8.38 -}
    8.39 +}}
    8.40  
    8.41  static SDL_Cursor *
    8.42  Cocoa_CreateSystemCursor(SDL_SystemCursor id)
    8.43 +{ @autoreleasepool
    8.44  {
    8.45 -    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    8.46      NSCursor *nscursor = NULL;
    8.47      SDL_Cursor *cursor = NULL;
    8.48  
    8.49 @@ -169,28 +165,23 @@
    8.50          }
    8.51      }
    8.52  
    8.53 -    [pool release];
    8.54 -
    8.55      return cursor;
    8.56 -}
    8.57 +}}
    8.58  
    8.59  static void
    8.60  Cocoa_FreeCursor(SDL_Cursor * cursor)
    8.61 +{ @autoreleasepool
    8.62  {
    8.63 -    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    8.64      NSCursor *nscursor = (NSCursor *)cursor->driverdata;
    8.65  
    8.66      [nscursor release];
    8.67      SDL_free(cursor);
    8.68 -
    8.69 -    [pool release];
    8.70 -}
    8.71 +}}
    8.72  
    8.73  static int
    8.74  Cocoa_ShowCursor(SDL_Cursor * cursor)
    8.75 +{ @autoreleasepool
    8.76  {
    8.77 -    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    8.78 -
    8.79      SDL_VideoDevice *device = SDL_GetVideoDevice();
    8.80      SDL_Window *window = (device ? device->windows : NULL);
    8.81      for (; window != NULL; window = window->next) {
    8.82 @@ -201,11 +192,8 @@
    8.83                                                  waitUntilDone:NO];
    8.84          }
    8.85      }
    8.86 -
    8.87 -    [pool release];
    8.88 -
    8.89      return 0;
    8.90 -}
    8.91 +}}
    8.92  
    8.93  static void
    8.94  Cocoa_WarpMouseGlobal(int x, int y)