Commit d1cc47b337adadf3f34ae3bcd585ef3e27258073

Sam Lantinga 2014-08-17T15:07:00

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
diff --git a/src/file/cocoa/SDL_rwopsbundlesupport.m b/src/file/cocoa/SDL_rwopsbundlesupport.m
index f6a65a9..1ae399c 100644
--- a/src/file/cocoa/SDL_rwopsbundlesupport.m
+++ b/src/file/cocoa/SDL_rwopsbundlesupport.m
@@ -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;
 
@@ -41,9 +42,6 @@ FILE* SDL_OpenFPFromBundleOrFallback(const char *file, const char *mode)
         return fopen(file, mode);
     }
 
-    NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
-
-
     NSFileManager* file_manager = [NSFileManager defaultManager];
     NSString* resource_path = [[NSBundle mainBundle] resourcePath];
 
@@ -57,10 +55,8 @@ FILE* SDL_OpenFPFromBundleOrFallback(const char *file, const char *mode)
         fp = fopen(file, mode);
     }
 
-    [autorelease_pool drain];
-
     return fp;
-}
+}}
 
 #endif /* __MACOSX__ */
 
diff --git a/src/filesystem/cocoa/SDL_sysfilesystem.m b/src/filesystem/cocoa/SDL_sysfilesystem.m
index adcebd6..dab51c5 100644
--- a/src/filesystem/cocoa/SDL_sysfilesystem.m
+++ b/src/filesystem/cocoa/SDL_sysfilesystem.m
@@ -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;
@@ -62,14 +62,13 @@ SDL_GetBasePath(void)
         }
     }
 
-    [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;
 
@@ -96,9 +95,8 @@ SDL_GetPrefPath(const char *org, const char *app)
         }
     }
 
-    [pool release];
     return retval;
-}
+}}
 
 #endif /* SDL_FILESYSTEM_COCOA */
 
diff --git a/src/video/cocoa/SDL_cocoaclipboard.m b/src/video/cocoa/SDL_cocoaclipboard.m
index c2f3be7..bf3b909 100644
--- a/src/video/cocoa/SDL_cocoaclipboard.m
+++ b/src/video/cocoa/SDL_cocoaclipboard.m
@@ -37,34 +37,28 @@ GetTextFormat(_THIS)
 
 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]) {
@@ -82,10 +76,8 @@ Cocoa_GetClipboardText(_THIS)
         text = SDL_strdup("");
     }
 
-    [pool release];
-
     return text;
-}
+}}
 
 SDL_bool
 Cocoa_HasClipboardText(_THIS)
@@ -101,13 +93,11 @@ Cocoa_HasClipboardText(_THIS)
 
 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) {
@@ -116,9 +106,7 @@ Cocoa_CheckClipboardUpdate(struct SDL_VideoData * data)
         }
         data->clipboard_count = count;
     }
-
-    [pool release];
-}
+}}
 
 #endif /* SDL_VIDEO_DRIVER_COCOA */
 
diff --git a/src/video/cocoa/SDL_cocoaevents.m b/src/video/cocoa/SDL_cocoaevents.m
index 3474d15..13aeb45 100644
--- a/src/video/cocoa/SDL_cocoaevents.m
+++ b/src/video/cocoa/SDL_cocoaevents.m
@@ -248,17 +248,16 @@ CreateApplicationMenus(void)
 
 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);
@@ -287,14 +286,12 @@ Cocoa_RegisterApp(void)
             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;
@@ -306,7 +303,6 @@ Cocoa_PumpEvents(_THIS)
         }
     }
 
-    pool = [[NSAutoreleasePool alloc] init];
     for ( ; ; ) {
         NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
         if ( event == nil ) {
@@ -338,8 +334,7 @@ Cocoa_PumpEvents(_THIS)
         /* Pass through to NSApp to make sure everything stays in sync */
         [NSApp sendEvent:event];
     }
-    [pool release];
-}
+}}
 
 #endif /* SDL_VIDEO_DRIVER_COCOA */
 
diff --git a/src/video/cocoa/SDL_cocoakeyboard.m b/src/video/cocoa/SDL_cocoakeyboard.m
index 53f5886..c621182 100644
--- a/src/video/cocoa/SDL_cocoakeyboard.m
+++ b/src/video/cocoa/SDL_cocoakeyboard.m
@@ -479,9 +479,9 @@ Cocoa_InitKeyboard(_THIS)
 
 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) {
@@ -506,23 +506,20 @@ Cocoa_StartTextInput(_THIS)
         [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)
diff --git a/src/video/cocoa/SDL_cocoamessagebox.m b/src/video/cocoa/SDL_cocoamessagebox.m
index 2ed7d90..5a8f780 100644
--- a/src/video/cocoa/SDL_cocoamessagebox.m
+++ b/src/video/cocoa/SDL_cocoamessagebox.m
@@ -79,11 +79,10 @@
 /* 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) {
@@ -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);
     }
 
-    [pool release];
-
     return returnValue;
-}
+}}
 
 #endif /* SDL_VIDEO_DRIVER_COCOA */
 
diff --git a/src/video/cocoa/SDL_cocoamodes.m b/src/video/cocoa/SDL_cocoamodes.m
index a98d376..5f46058 100644
--- a/src/video/cocoa/SDL_cocoamodes.m
+++ b/src/video/cocoa/SDL_cocoamodes.m
@@ -214,8 +214,8 @@ Cocoa_GetDisplayName(CGDirectDisplayID displayID)
 
 void
 Cocoa_InitModes(_THIS)
+{ @autoreleasepool
 {
-    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     CGDisplayErr result;
     CGDirectDisplayID *displays;
     CGDisplayCount numDisplays;
@@ -224,7 +224,6 @@ Cocoa_InitModes(_THIS)
     result = CGGetOnlineDisplayList(0, NULL, &numDisplays);
     if (result != kCGErrorSuccess) {
         CG_SetError("CGGetOnlineDisplayList()", result);
-        [pool release];
         return;
     }
     displays = SDL_stack_alloc(CGDirectDisplayID, numDisplays);
@@ -232,7 +231,6 @@ Cocoa_InitModes(_THIS)
     if (result != kCGErrorSuccess) {
         CG_SetError("CGGetOnlineDisplayList()", result);
         SDL_stack_free(displays);
-        [pool release];
         return;
     }
 
@@ -297,8 +295,7 @@ Cocoa_InitModes(_THIS)
         }
     }
     SDL_stack_free(displays);
-    [pool release];
-}
+}}
 
 int
 Cocoa_GetDisplayBounds(_THIS, SDL_VideoDisplay * display, SDL_Rect * rect)
diff --git a/src/video/cocoa/SDL_cocoamouse.m b/src/video/cocoa/SDL_cocoamouse.m
index f048060..8b933c2 100644
--- a/src/video/cocoa/SDL_cocoamouse.m
+++ b/src/video/cocoa/SDL_cocoamouse.m
@@ -66,8 +66,8 @@
 
 static SDL_Cursor *
 Cocoa_CreateDefaultCursor()
+{ @autoreleasepool
 {
-    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     NSCursor *nscursor;
     SDL_Cursor *cursor = NULL;
 
@@ -81,15 +81,13 @@ Cocoa_CreateDefaultCursor()
         }
     }
 
-    [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;
@@ -108,15 +106,13 @@ Cocoa_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
         }
     }
 
-    [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;
 
@@ -169,28 +165,23 @@ Cocoa_CreateSystemCursor(SDL_SystemCursor id)
         }
     }
 
-    [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) {
@@ -201,11 +192,8 @@ Cocoa_ShowCursor(SDL_Cursor * cursor)
                                                 waitUntilDone:NO];
         }
     }
-
-    [pool release];
-
     return 0;
-}
+}}
 
 static void
 Cocoa_WarpMouseGlobal(int x, int y)