Skip to content

Instantly share code, notes, and snippets.

@probablycorey
Created February 18, 2011 19:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save probablycorey/834223 to your computer and use it in GitHub Desktop.
Save probablycorey/834223 to your computer and use it in GitHub Desktop.
WaxRetainIssueAppDelegate.m
//
// wax_garbage_collection.m
// WaxTests
//
// Created by Corey Johnson on 2/23/10.
// Copyright 2010 Probably Interactive. All rights reserved.
//
#import "wax_gc.h"
#import "lua.h"
#import "lauxlib.h"
#import "wax.h"
#import "wax_instance.h"
#import "wax_helpers.h"
#define WAX_GC_TIMEOUT 1
@implementation wax_gc
+ (void)start {
[NSTimer scheduledTimerWithTimeInterval:WAX_GC_TIMEOUT target:self selector:@selector(cleanupUnusedObject) userInfo:nil repeats:YES];
}
+ (void)cleanupUnusedObject {
lua_State *L = wax_currentLuaState();
BEGIN_STACK_MODIFY(L)
wax_instance_pushStrongUserdataTable(L);
lua_pushnil(L); // first key
while (lua_next(L, -2)) {
wax_instance_userdata *instanceUserdata = (wax_instance_userdata *)luaL_checkudata(L, -1, WAX_INSTANCE_METATABLE_NAME);
lua_pop(L, 1); // pops the value, keeps the key
if (!instanceUserdata->isClass && !instanceUserdata->isSuper && [instanceUserdata->instance retainCount] <= 1) {
NSLog(@"Releasing %@", instanceUserdata->instance);
lua_pushvalue(L, -1);
lua_pushnil(L);
lua_rawset(L, -4); // Clear it!
}
}
END_STACK_MODIFY(L, 0);
}
@end
//
// WaxRetainIssueAppDelegate.m
// WaxRetainIssue
//
// Created by Martin Cote on 11-02-17.
// Copyright Loopycube 2011. All rights reserved.
//
#import "WaxRetainIssueAppDelegate.h"
#import "WaxRetainIssueViewController.h"
#import "MyClass.h"
#import "wax.h"
#import "lua.h"
#import "lauxlib.h"
@implementation WaxRetainIssueAppDelegate
@synthesize window;
@synthesize viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/////////////////////////////////////////////////////////////////////////////////
// Action here!
wax_start();
luaL_dofile(wax_currentLuaState(), "script.lua");
MyClass *o = [[MyClass alloc] init];
Class LuaClass = NSClassFromString(@"LuaClass");
id luaObject = [[LuaClass alloc] init];
[luaObject useObject:o];
[luaObject release];
[o release]; // <--- dealloc is not called!
/////////////////////////////////////////////////////////////////////////////////
[self performSelector:@selector(forceLuaGC) withObject:nil afterDelay:5];
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
- (void)forceLuaGC {
NSLog(@"Running GC");
lua_gc(wax_currentLuaState(), LUA_GCCOLLECT, nil);
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment