Skip to content

Commit 0f39305

Browse files
committed
Avoid directly calling objc runtime functions in C
By managing autorelease pools in objc code we avoid warnings on OS X 10.9 and it is also safer than calling objc runtime functions from C.
1 parent bc25430 commit 0f39305

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

src/MacVim/gui_macvim.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,6 +1806,15 @@ void gui_macvim_get_window_layout(int *count, int *layout)
18061806
}
18071807
}
18081808

1809+
void *gui_macvim_new_autoreleasepool()
1810+
{
1811+
return (void *)[[NSAutoreleasePool alloc] init];
1812+
}
1813+
1814+
void gui_macvim_release_autoreleasepool(void *pool)
1815+
{
1816+
[(id)pool release];
1817+
}
18091818

18101819
// -- Client/Server ---------------------------------------------------------
18111820

src/main.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323
# include <limits.h>
2424
#endif
2525

26-
#ifdef FEAT_GUI_MACVIM
27-
#include <objc/objc-runtime.h> /* for objc_*() and sel_*() */
28-
#endif
29-
3026
/* Maximum number of commands from + or -c arguments. */
3127
#define MAX_ARG_CMDS 10
3228

@@ -180,9 +176,7 @@ main
180176
// Cocoa needs an NSAutoreleasePool in place or it will leak memory.
181177
// This particular pool will hold autorelease objects created during
182178
// initialization.
183-
id autoreleasePool = objc_msgSend(objc_msgSend(
184-
objc_getClass("NSAutoreleasePool"),sel_getUid("alloc")
185-
), sel_getUid("init"));
179+
void *autoreleasePool = gui_macvim_new_autoreleasepool();
186180
#endif
187181

188182
/*
@@ -1066,13 +1060,11 @@ vim_main2(int argc UNUSED, char **argv UNUSED)
10661060
#ifdef FEAT_GUI_MACVIM
10671061
// The autorelease pool might have filled up quite a bit during
10681062
// initialization, so purge it before entering the main loop.
1069-
objc_msgSend(autoreleasePool, sel_getUid("release"));
1063+
gui_macvim_release_autoreleasepool(autoreleasePool);
10701064

10711065
// The main loop sets up its own autorelease pool, but to be safe we still
10721066
// realloc this one here.
1073-
autoreleasePool = objc_msgSend(objc_msgSend(
1074-
objc_getClass("NSAutoreleasePool"),sel_getUid("alloc")
1075-
), sel_getUid("init"));
1067+
autoreleasePool = gui_macvim_new_autoreleasepool();
10761068
#endif
10771069

10781070
/*
@@ -1081,7 +1073,7 @@ vim_main2(int argc UNUSED, char **argv UNUSED)
10811073
main_loop(FALSE, FALSE);
10821074

10831075
#ifdef FEAT_GUI_MACVIM
1084-
objc_msgSend(autoreleasePool, sel_getUid("release"));
1076+
gui_macvim_release_autoreleasepool(autoreleasePool);
10851077
#endif
10861078

10871079
return 0;
@@ -1152,9 +1144,7 @@ main_loop(cmdwin, noexmode)
11521144
#ifdef FEAT_GUI_MACVIM
11531145
// Cocoa needs an NSAutoreleasePool in place or it will leak memory.
11541146
// This particular pool gets released once every loop.
1155-
id autoreleasePool = objc_msgSend(objc_msgSend(
1156-
objc_getClass("NSAutoreleasePool"),sel_getUid("alloc")
1157-
), sel_getUid("init"));
1147+
void *autoreleasePool = gui_macvim_new_autoreleasepool();
11581148
#endif
11591149

11601150
if (stuff_empty())
@@ -1404,7 +1394,7 @@ main_loop(cmdwin, noexmode)
14041394
#ifdef FEAT_GUI_MACVIM
14051395
// TODO! Make sure there are no continue statements that will cause
14061396
// this not to be called or MacVim will leak memory!
1407-
objc_msgSend(autoreleasePool, sel_getUid("release"));
1397+
gui_macvim_release_autoreleasepool(autoreleasePool);
14081398
#endif
14091399
}
14101400
}

src/proto/gui_macvim.pro

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,6 @@ gui_mch_register_sign(char_u *signfile);
236236

237237
void
238238
gui_mch_destroy_sign(void *sign);
239+
240+
void *gui_macvim_new_autoreleasepool();
241+
void gui_macvim_release_autoreleasepool(void *pool);

0 commit comments

Comments
 (0)