From 6b08c0797190d79f89db1300ba80008852316f96 Mon Sep 17 00:00:00 2001 From: Tu Yimin Date: Sat, 25 Aug 2012 14:20:47 +0800 Subject: [PATCH] fix leak issue --- README.md | 4 ---- WaxPatch/WaxPatch/wax/wax_gc.m | 3 +++ WaxPatch/WaxPatch/wax/wax_instance.h | 1 + WaxPatch/WaxPatch/wax/wax_instance.m | 6 ++++-- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 16e99ac..f38300b 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,3 @@ The patch is a zip file contains patch.lua and other lua codes. The sample code The sample iOS project loads the patch from a url (which you probably want to change in AppDelegate.m) before launch. The Original.png and Patched.png shows the difference. -### Issue -The mutated version of wax leaks when call method directly from Obj-C!!! - -The issue is located at wax_instance.m, TODO mark. diff --git a/WaxPatch/WaxPatch/wax/wax_gc.m b/WaxPatch/WaxPatch/wax/wax_gc.m index fc03f9a..d0db942 100755 --- a/WaxPatch/WaxPatch/wax/wax_gc.m +++ b/WaxPatch/WaxPatch/wax/wax_gc.m @@ -46,6 +46,9 @@ + (void)cleanupUnusedObject { lua_pushvalue(L, -1); lua_pushnil(L); lua_rawset(L, -4); // Clear it! + if(instanceUserdata->waxRetain) { + [instanceUserdata->instance release]; + } } } diff --git a/WaxPatch/WaxPatch/wax/wax_instance.h b/WaxPatch/WaxPatch/wax/wax_instance.h index d9db36e..e8bcb4e 100755 --- a/WaxPatch/WaxPatch/wax/wax_instance.h +++ b/WaxPatch/WaxPatch/wax/wax_instance.h @@ -20,6 +20,7 @@ typedef struct _wax_instance_userdata { BOOL isClass; Class isSuper; // isSuper not only stores whether the class is a super, but it also contains the value of the next superClass. BOOL actAsSuper; // It only acts like a super once, when it is called for the first time. + BOOL waxRetain; // retained by wax, release instance when wax_gc } wax_instance_userdata; int luaopen_wax_instance(lua_State *L); diff --git a/WaxPatch/WaxPatch/wax/wax_instance.m b/WaxPatch/WaxPatch/wax/wax_instance.m index a9b43fb..4bc1395 100755 --- a/WaxPatch/WaxPatch/wax/wax_instance.m +++ b/WaxPatch/WaxPatch/wax/wax_instance.m @@ -218,8 +218,10 @@ BOOL wax_instance_pushFunction(lua_State *L, id self, SEL selector) { if (lua_isnil(L, -1)) { // TODO: // quick and dirty solution to let obj-c call directly into lua - // cause a obj-c leak, should we release it later? - wax_instance_create(L, self, NO); + // to avoid obj-c leak, we release instance when wax_gc + wax_instance_userdata *data = wax_instance_create(L, self, NO); + data->waxRetain = YES; +// [self release]; // END_STACK_MODIFY(L, 0) // return NO; // userdata doesn't exist }