public void Initialize() { Default = new LuaEnv(); LuaClassCache = new LuaClassCache(); m_newClassCallback = OnLuaNewClass; #if UNITY_EDITOR Default.AddLoader((ref string filename) => LoadFile(ref filename, ".lua")); #else Default.AddLoader((ref string filename) => LoadFile(ref filename, ".lua")); #endif var setupNewCallback = LoadFileAtPath("base.class")[0] as LuaFunction; setupNewCallback.Action(m_newClassCallback); OnVMCreated?.Invoke(); var ret = LoadFileAtPath("PreRequest")[0] as LuaTable; OnInit = ret.Get <LuaFunction>("init"); OnDestroy = ret.Get <LuaFunction>("shutdown"); OnPreRequestLoaded?.Invoke(); #if UNITY_EDITOR if (reportLeakMark) { leakData = Default.StartMemoryLeakCheck(); } #endif AssetService.Get().AddAfterDestroy(this); DestroyedTableMeta = Global.Get <LuaTable>("DestroyedTableMeta"); }
void Start() { luaenv.DoString(@" local local_leak = {} global_leak = { a = {}} --global_leak.a.b = global_leak local no_leak = {} function make_leak1() table.insert(local_leak, 1) table.insert(global_leak, {}) end -- 会不断创建并持有新table,但其实没泄漏 function innocent() no_leak.a = {x = 1} no_leak.b = {y = 1} end ", "@leak1.lua"); luaenv.DoString(@" local anthor_leak = {a = {{ b = {}}}} function make_leak2() table.insert(anthor_leak.a[1].b, 1) end local t = 1 slow_global_leak = {} debug.getregistry()['ref_anthor_leak'] = anthor_leak function slow_leak() if t == 40 then t = 0 table.insert(slow_global_leak, {x = 0, y = 1}) else t = t + 1 end end ", "@leak2.lua"); luaenv.DoString(@" shutdown_fast_leak = false function update() if not shutdown_fast_leak then make_leak1() make_leak2() end innocent() slow_leak() end ", "@main.lua"); luaenv.Global.Get("update", out update); data = luaenv.StartMemoryLeakCheck(); Debug.Log("Start, PotentialLeakCount:" + data.PotentialLeakCount); }