Skip to content

Commit 492b9c4

Browse files
committed
ui_cocoa: converge HAVE_COCOA and HAVE_COCOA_METAL on ARC/MRR
21 preprocessor blocks in ui_cocoa.m guarded [... release] and [... autorelease] calls behind `#ifndef HAVE_COCOA_METAL`. Plus one -dealloc override gated on the same. This was using HAVE_COCOA_METAL as a proxy for "are we compiled under ARC?". The proxy is brittle: Build target ARC? MRR? qb/make (any) no YES (ui_cocoa.m always MRR; only metal.m + mfi_joypad.m get -fobjc-arc per Makefile lines 275-276) RetroArch.xcodeproj YES no (BaseConfig.xcconfig:182 RetroArch_Metal.xcodeproj YES no CLANG_ENABLE_OBJC_ARC=YES RetroArch_OSX107.xcodeproj YES no cascades down) RetroArch_PPC.xcodeproj no YES (explicit override to NO) The HAVE_COCOA_METAL-based guard happens to be right for 3 of these 5 by coincidence (Metal xcodeproj + PPC xcodeproj + qb/make), but wrong for the two that flip the correlation. The clang- documented discriminator is __has_feature(objc_arc), which is exactly true under ARC and false under MRR regardless of other build options. Add three macros in cocoa_defines.h that expand to the right thing either way: RARCH_RELEASE(x) [(x) release] / ((void)0) under ARC RARCH_AUTORELEASE(x) [(x) autorelease] / ((void)0) under ARC RARCH_SUPER_DEALLOC() [super dealloc] / ((void)0) under ARC Wrapped in the standard __has_feature polyfill so GCC 4.0 (Xcode 3.1, which predates ARC and __has_feature both) falls through to the MRR branch, which is correct. Replace 21 conditional release/autorelease blocks with unconditional macro calls. Gate the -dealloc override on #if !__has_feature (objc_arc) instead of #ifndef HAVE_COCOA_METAL - ARC auto-generates -dealloc from strong ivars and forbids explicit overrides that just call [super dealloc], so the method stays conditional but on the correct axis. The one remaining #ifndef HAVE_COCOA_METAL site (now line 646, the addSubview:[CocoaView get] / makeFirstResponder: block) is a legitimate platform branch - Metal installs CocoaView via apple_platform.renderView through a different codepath - and stays put. Net change: -10 LoC, -21 preprocessor blocks, zero runtime behaviour change on any of the five build configurations.
1 parent 623d387 commit 492b9c4

2 files changed

Lines changed: 63 additions & 73 deletions

File tree

libretro-common/include/defines/cocoa_defines.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,33 @@
7373
#define HAS_MACOSX_10_12 1
7474
#endif
7575

76+
/* ARC vs MRR macros. Under ARC, release/autorelease are forbidden
77+
* (compile error); under MRR (Manual Retain-Release / MRC) they are
78+
* required. These macros expand to the right thing for the current
79+
* translation unit regardless of which mode it is compiled under.
80+
*
81+
* Previous convention used `#ifndef HAVE_COCOA_METAL` as a proxy
82+
* for 'are we under ARC?' because HAVE_COCOA_METAL builds happened
83+
* to enable ARC via CLANG_ENABLE_OBJC_ARC=YES in BaseConfig.xcconfig.
84+
* That's brittle - RetroArch_PPC.xcodeproj turns ARC off explicitly
85+
* even on the Metal branch, and the qb/make build is always MRR
86+
* regardless. __has_feature(objc_arc) is the canonical discriminator.
87+
*
88+
* GCC 4.0 (Xcode 3.1) doesn't support __has_feature; polyfill to 0
89+
* so the MRR branch is selected (which is correct for GCC 4.0 - it
90+
* predates ARC entirely). */
91+
#ifndef __has_feature
92+
#define __has_feature(x) 0
93+
#endif
94+
95+
#if __has_feature(objc_arc)
96+
#define RARCH_RELEASE(x) ((void)0)
97+
#define RARCH_AUTORELEASE(x) ((void)0)
98+
#define RARCH_SUPER_DEALLOC() ((void)0)
99+
#else
100+
#define RARCH_RELEASE(x) [(x) release]
101+
#define RARCH_AUTORELEASE(x) [(x) autorelease]
102+
#define RARCH_SUPER_DEALLOC() [super dealloc]
103+
#endif
104+
76105
#endif

ui/drivers/ui_cocoa.m

Lines changed: 34 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -609,11 +609,14 @@ @implementation RetroArch_OSX
609609

610610
@synthesize window = _window;
611611

612-
#ifndef HAVE_COCOA_METAL
612+
#if !__has_feature(objc_arc)
613+
/* ARC auto-generates -dealloc from strong ivars and forbids explicit
614+
* overrides that just call [super dealloc]. On MRR we have to release
615+
* _window manually. See cocoa_defines.h for the ARC/MRR macro story. */
613616
- (void)dealloc
614617
{
615-
[_window release];
616-
[super dealloc];
618+
RARCH_RELEASE(_window);
619+
RARCH_SUPER_DEALLOC();
617620
}
618621
#endif
619622

@@ -1119,9 +1122,7 @@ - (void)alertDidEnd:(NSAlert *)alert returnCode:(int32_t)returnCode contextInfo:
11191122
[item setTarget:target];
11201123
if (tag)
11211124
[item setTag:tag];
1122-
#ifndef HAVE_COCOA_METAL
1123-
[item autorelease];
1124-
#endif
1125+
RARCH_AUTORELEASE(item);
11251126
return item;
11261127
}
11271128

@@ -1140,10 +1141,8 @@ - (void)alertDidEnd:(NSAlert *)alert returnCode:(int32_t)returnCode contextInfo:
11401141
[NSApp setServicesMenu:servicesMenu];
11411142
[servicesItem setSubmenu:servicesMenu];
11421143
[menu addItem:servicesItem];
1143-
#ifndef HAVE_COCOA_METAL
1144-
[servicesItem release];
1145-
[servicesMenu release];
1146-
#endif
1144+
RARCH_RELEASE(servicesItem);
1145+
RARCH_RELEASE(servicesMenu);
11471146

11481147
[menu addItem:[NSMenuItem separatorItem]];
11491148
[menu addItem:cocoa_menu_item_with_action(@"Hide RetroArch",
@@ -1157,9 +1156,7 @@ - (void)alertDidEnd:(NSAlert *)alert returnCode:(int32_t)returnCode contextInfo:
11571156
[menu addItem:cocoa_menu_item_with_action(@"Quit RetroArch",
11581157
@selector(terminate:), @"q", NSEventModifierFlagCommand, NSApp, 0)];
11591158

1160-
#ifndef HAVE_COCOA_METAL
1161-
[menu autorelease];
1162-
#endif
1159+
RARCH_AUTORELEASE(menu);
11631160
return menu;
11641161
}
11651162

@@ -1181,18 +1178,14 @@ - (void)alertDidEnd:(NSAlert *)alert returnCode:(int32_t)returnCode contextInfo:
11811178
NSMenuItem *clearItem = cocoa_menu_item_with_action(@"Clear Menu",
11821179
@selector(clearRecentDocuments:), @"", 0, nil, 0);
11831180
[recentMenu addItem:clearItem];
1184-
#ifndef HAVE_COCOA_METAL
1185-
[recentItem release];
1186-
[recentMenu release];
1187-
#endif
1181+
RARCH_RELEASE(recentItem);
1182+
RARCH_RELEASE(recentMenu);
11881183

11891184
[menu addItem:[NSMenuItem separatorItem]];
11901185
[menu addItem:cocoa_menu_item_with_action(@"Close",
11911186
@selector(performClose:), @"w", NSEventModifierFlagCommand, nil, 0)];
11921187

1193-
#ifndef HAVE_COCOA_METAL
1194-
[menu autorelease];
1195-
#endif
1188+
RARCH_AUTORELEASE(menu);
11961189
return menu;
11971190
}
11981191

@@ -1209,10 +1202,8 @@ - (void)alertDidEnd:(NSAlert *)alert returnCode:(int32_t)returnCode contextInfo:
12091202
@selector(basicEvent:), @"", 0, delegate, 22)];
12101203
[audioItem setSubmenu:audioMenu];
12111204
[menu addItem:audioItem];
1212-
#ifndef HAVE_COCOA_METAL
1213-
[audioItem release];
1214-
[audioMenu release];
1215-
#endif
1205+
RARCH_RELEASE(audioItem);
1206+
RARCH_RELEASE(audioMenu);
12161207

12171208
/* Disk Options submenu */
12181209
NSMenuItem *diskItem = [[NSMenuItem alloc] initWithTitle:@"Disk Options"
@@ -1227,10 +1218,8 @@ - (void)alertDidEnd:(NSAlert *)alert returnCode:(int32_t)returnCode contextInfo:
12271218
@selector(basicEvent:), @"", 0, delegate, 5)];
12281219
[diskItem setSubmenu:diskMenu];
12291220
[menu addItem:diskItem];
1230-
#ifndef HAVE_COCOA_METAL
1231-
[diskItem release];
1232-
[diskMenu release];
1233-
#endif
1221+
RARCH_RELEASE(diskItem);
1222+
RARCH_RELEASE(diskMenu);
12341223

12351224
/* Mouse Options submenu */
12361225
NSMenuItem *mouseItem = [[NSMenuItem alloc] initWithTitle:@"Mouse Options"
@@ -1241,10 +1230,8 @@ - (void)alertDidEnd:(NSAlert *)alert returnCode:(int32_t)returnCode contextInfo:
12411230
@selector(basicEvent:), @"", 0, delegate, 7)];
12421231
[mouseItem setSubmenu:mouseMenu];
12431232
[menu addItem:mouseItem];
1244-
#ifndef HAVE_COCOA_METAL
1245-
[mouseItem release];
1246-
[mouseMenu release];
1247-
#endif
1233+
RARCH_RELEASE(mouseItem);
1234+
RARCH_RELEASE(mouseMenu);
12481235

12491236
/* Save State Options submenu */
12501237
NSMenuItem *stateItem = [[NSMenuItem alloc] initWithTitle:@"Save State Options"
@@ -1257,10 +1244,8 @@ - (void)alertDidEnd:(NSAlert *)alert returnCode:(int32_t)returnCode contextInfo:
12571244
@selector(basicEvent:), @"", 0, delegate, 3)];
12581245
[stateItem setSubmenu:stateMenu];
12591246
[menu addItem:stateItem];
1260-
#ifndef HAVE_COCOA_METAL
1261-
[stateItem release];
1262-
[stateMenu release];
1263-
#endif
1247+
RARCH_RELEASE(stateItem);
1248+
RARCH_RELEASE(stateMenu);
12641249

12651250
[menu addItem:cocoa_menu_item_with_action(@"Reset",
12661251
@selector(basicEvent:), @"", 0, delegate, 1)];
@@ -1271,9 +1256,7 @@ - (void)alertDidEnd:(NSAlert *)alert returnCode:(int32_t)returnCode contextInfo:
12711256
[menu addItem:cocoa_menu_item_with_action(@"Take Screenshot",
12721257
@selector(basicEvent:), @"", 0, delegate, 21)];
12731258

1274-
#ifndef HAVE_COCOA_METAL
1275-
[menu autorelease];
1276-
#endif
1259+
RARCH_AUTORELEASE(menu);
12771260
return menu;
12781261
}
12791262

@@ -1282,9 +1265,7 @@ - (void)alertDidEnd:(NSAlert *)alert returnCode:(int32_t)returnCode contextInfo:
12821265
NSMenu *menu = [[NSMenu alloc] initWithTitle:@"Paths"];
12831266
[menu addItem:cocoa_menu_item_with_action(@"Core Directory",
12841267
@selector(showCoresDirectory:), @"", 0, delegate, 0)];
1285-
#ifndef HAVE_COCOA_METAL
1286-
[menu autorelease];
1287-
#endif
1268+
RARCH_AUTORELEASE(menu);
12881269
return menu;
12891270
}
12901271

@@ -1311,10 +1292,8 @@ - (void)alertDidEnd:(NSAlert *)alert returnCode:(int32_t)returnCode contextInfo:
13111292
}
13121293
[scaleItem setSubmenu:scaleMenu];
13131294
[menu addItem:scaleItem];
1314-
#ifndef HAVE_COCOA_METAL
1315-
[scaleItem release];
1316-
[scaleMenu release];
1317-
#endif
1295+
RARCH_RELEASE(scaleItem);
1296+
RARCH_RELEASE(scaleMenu);
13181297

13191298
[menu addItem:cocoa_menu_item_with_action(@"Enter Full Screen",
13201299
@selector(toggleFullScreen:), @"f",
@@ -1327,9 +1306,7 @@ - (void)alertDidEnd:(NSAlert *)alert returnCode:(int32_t)returnCode contextInfo:
13271306

13281307
[NSApp setWindowsMenu:menu];
13291308

1330-
#ifndef HAVE_COCOA_METAL
1331-
[menu autorelease];
1332-
#endif
1309+
RARCH_AUTORELEASE(menu);
13331310
return menu;
13341311
}
13351312

@@ -1345,9 +1322,7 @@ - (void)alertDidEnd:(NSAlert *)alert returnCode:(int32_t)returnCode contextInfo:
13451322
* for-Help into. */
13461323
if ([NSApp respondsToSelector:@selector(setHelpMenu:)])
13471324
[NSApp setHelpMenu:menu];
1348-
#ifndef HAVE_COCOA_METAL
1349-
[menu autorelease];
1350-
#endif
1325+
RARCH_AUTORELEASE(menu);
13511326
return menu;
13521327
}
13531328

@@ -1362,54 +1337,40 @@ static void cocoa_create_menu_bar(id delegate)
13621337
submenu = cocoa_create_app_menu(delegate);
13631338
[item setSubmenu:submenu];
13641339
[menubar addItem:item];
1365-
#ifndef HAVE_COCOA_METAL
1366-
[item release];
1367-
#endif
1340+
RARCH_RELEASE(item);
13681341

13691342
/* File menu */
13701343
item = [[NSMenuItem alloc] init];
13711344
[item setSubmenu:cocoa_create_file_menu(delegate)];
13721345
[menubar addItem:item];
1373-
#ifndef HAVE_COCOA_METAL
1374-
[item release];
1375-
#endif
1346+
RARCH_RELEASE(item);
13761347

13771348
/* Command menu */
13781349
item = [[NSMenuItem alloc] init];
13791350
[item setSubmenu:cocoa_create_command_menu(delegate)];
13801351
[menubar addItem:item];
1381-
#ifndef HAVE_COCOA_METAL
1382-
[item release];
1383-
#endif
1352+
RARCH_RELEASE(item);
13841353

13851354
/* Paths menu */
13861355
item = [[NSMenuItem alloc] init];
13871356
[item setSubmenu:cocoa_create_paths_menu(delegate)];
13881357
[menubar addItem:item];
1389-
#ifndef HAVE_COCOA_METAL
1390-
[item release];
1391-
#endif
1358+
RARCH_RELEASE(item);
13921359

13931360
/* Window menu */
13941361
item = [[NSMenuItem alloc] init];
13951362
[item setSubmenu:cocoa_create_window_menu(delegate)];
13961363
[menubar addItem:item];
1397-
#ifndef HAVE_COCOA_METAL
1398-
[item release];
1399-
#endif
1364+
RARCH_RELEASE(item);
14001365

14011366
/* Help menu */
14021367
item = [[NSMenuItem alloc] init];
14031368
[item setSubmenu:cocoa_create_help_menu()];
14041369
[menubar addItem:item];
1405-
#ifndef HAVE_COCOA_METAL
1406-
[item release];
1407-
#endif
1370+
RARCH_RELEASE(item);
14081371

14091372
[NSApp setMainMenu:menubar];
1410-
#ifndef HAVE_COCOA_METAL
1411-
[menubar release];
1412-
#endif
1373+
RARCH_RELEASE(menubar);
14131374
}
14141375

14151376
static NSWindow *cocoa_create_main_window(void)

0 commit comments

Comments
 (0)