Skip to content

Commit

Permalink
修复清理纹理引用遗留BUG。
Browse files Browse the repository at this point in the history
  • Loading branch information
jarjin committed Nov 30, 2015
1 parent ac566bb commit be9835d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
40 changes: 38 additions & 2 deletions Assets/uLua/Core/MethodWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace LuaInterface
namespace LuaInterface
{
using System;
using System.IO;
Expand Down Expand Up @@ -35,7 +35,9 @@ public MethodBase cachedMethod

public bool IsReturnVoid;

// List or arguments
// List or arguments,
// fjs: 狗日的这个会缓存每一次调用某个函数的参数,导致内存释放不了。
// 修改方案: 调用完成后,或中间出错退出时一定要清空这个数组的所有元素, 完事以后一定得提起裤子就走人,别留恋!
public object[] args;
// Positions of out parameters
public int[] outList;
Expand Down Expand Up @@ -132,6 +134,15 @@ private static bool IsInteger(double x) {
return Math.Ceiling(x) == x;
}

// fjs: 清空缓存的 args 数组,否则这里会造成内存泄露,无法被GC掉, 纹理内存最严重
private void ClearCachedArgs()
{
if(_LastCalledMethod.args == null) { return ; }
for(int i = 0; i < _LastCalledMethod.args.Length; i++)
{
_LastCalledMethod.args[i] = null;
}
}

/*
* Calls the method. Receives the arguments from the Lua stack
Expand Down Expand Up @@ -170,6 +181,7 @@ public int call(IntPtr luaState)
if (!LuaDLL.lua_checkstack(luaState, _LastCalledMethod.outList.Length + 6))
throw new LuaException("Lua stack overflow");

// fjs: 这里 args 只是将 _LastCalledMethod.args 拿来做缓冲区用,避免内存再分配, 里面的值是可以干掉的
object[] args = _LastCalledMethod.args;

try
Expand Down Expand Up @@ -247,6 +259,7 @@ public int call(IntPtr luaState)

MethodBase m = (MethodInfo)member;

// fjs: 这里在缓存调用参数,退出前一定要释放掉
bool isMethod = _Translator.matchParameters(luaState, m, ref _LastCalledMethod);
if (isMethod)
{
Expand All @@ -262,6 +275,10 @@ public int call(IntPtr luaState)

LuaDLL.luaL_error(luaState, msg);
LuaDLL.lua_pushnil(luaState);

// fjs: 这里释放掉缓存的参数对象
ClearCachedArgs();

return 1;
}
}
Expand All @@ -271,6 +288,7 @@ public int call(IntPtr luaState)
if (methodToCall.ContainsGenericParameters)
{
// bool isMethod = //* not used
// fjs: 这里在缓存调用参数,退出前一定要释放掉
_Translator.matchParameters(luaState, methodToCall, ref _LastCalledMethod);

if (methodToCall.IsGenericMethodDefinition)
Expand All @@ -290,6 +308,9 @@ public int call(IntPtr luaState)
{
LuaDLL.luaL_error(luaState, "unable to invoke method on generic class as the current method is an open generic method");
LuaDLL.lua_pushnil(luaState);

// fjs: 这里释放掉缓存的参数对象
ClearCachedArgs();
return 1;
}
}
Expand All @@ -301,10 +322,14 @@ public int call(IntPtr luaState)
LuaDLL.lua_remove(luaState, 1); // Pops the receiver
}

// fjs: 这里在缓存调用参数,退出前一定要释放掉
if (!_Translator.matchParameters(luaState, methodToCall, ref _LastCalledMethod))
{
LuaDLL.luaL_error(luaState, "invalid arguments to method call");
LuaDLL.lua_pushnil(luaState);

// fjs: 这里释放掉缓存的参数对象
ClearCachedArgs();
return 1;
}
}
Expand All @@ -313,7 +338,11 @@ public int call(IntPtr luaState)
if (failedCall)
{
if (!LuaDLL.lua_checkstack(luaState, _LastCalledMethod.outList.Length + 6))
{
// fjs: 这里释放掉缓存的参数对象
ClearCachedArgs();
throw new LuaException("Lua stack overflow");
}
try
{
if (isStatic)
Expand All @@ -330,10 +359,14 @@ public int call(IntPtr luaState)
}
catch (TargetInvocationException e)
{
// fjs: 这里释放掉缓存的参数对象
ClearCachedArgs();
return SetPendingException(e.GetBaseException());
}
catch (Exception e)
{
// fjs: 这里释放掉缓存的参数对象
ClearCachedArgs();
return SetPendingException(e);
}
}
Expand All @@ -355,6 +388,9 @@ public int call(IntPtr luaState)
nReturnValues++;
}

// fjs: 这里释放掉缓存的参数对象
ClearCachedArgs();

return nReturnValues < 1 ? 1 : nReturnValues;
}
}
Expand Down
9 changes: 7 additions & 2 deletions Assets/uLua/Core/ObjectTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ namespace LuaInterface
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using UnityEngine;
//using System.Diagnostics;

public static class ObjectExtends {
public static object RefObject(this object obj) {
return new WeakReference(obj).Target;
}
}

/*
* Passes objects from the CLR to Lua and vice-versa
*
Expand Down Expand Up @@ -632,7 +639,6 @@ public void pushObject(IntPtr luaState, object o, string metatable)
int index = -1;
// Object already in the list of Lua objects? Push the stored reference.
bool beValueType = o.GetType().IsValueType;

if (!beValueType && objectsBackMap.TryGetValue(o, out index))
{
if (LuaDLL.tolua_pushudata(luaState, weakTableRef, index))
Expand All @@ -648,7 +654,6 @@ public void pushObject(IntPtr luaState, object o, string metatable)
// Remove from both our tables and fall out to get a new ID
collectObject(o, index);
}

index = addObject(o, beValueType);
pushNewObject(luaState, o, index, metatable);
}
Expand Down
4 changes: 2 additions & 2 deletions Assets/uLua/Editor/WelcomeScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static void Update() {
public class WelcomeScreen : EditorWindow
{
private bool flag = true;
private string version = "Version : 1.23";
private string version = "Version : 1.24";
private Rect mContactDescriptionRect = new Rect(70f, 344f, 250f, 30f);
private Rect mContactHeaderRect = new Rect(70f, 324f, 250f, 20f);
private Texture mContactImage;
Expand Down Expand Up @@ -83,7 +83,7 @@ public void OnGUI()
GUI.Label(this.mForumDescriptionRect, "单击Lua菜单里面Encode LuaFile with UTF-8子菜单.");
GUI.DrawTexture(this.mContactImageRect, this.mContactImage);
GUI.Label(this.mContactHeaderRect, " 加入技术支持社群");
GUI.Label(this.mContactDescriptionRect, "QQ群:469941220 或者 QQ群:62978170");
GUI.Label(this.mContactDescriptionRect, "QQ群:434341400 或者 QQ群:62978170");
GUI.Label(this.mVersionRect, version );

flag = GUI.Toggle(this.mToggleButtonRect, flag, "开始时候显示对话框");
Expand Down
3 changes: 3 additions & 0 deletions ReadMe.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
游戏案例地址 http://www.ulua.org/showcase.html
框架详细介绍 http://bbs.ulua.org/default.asp?cateID=4

//-------------2015-11-28-------------
(1)修复清理纹理引用遗留BUG。

//-------------2015-11-10-------------
(1)添加lua中使用self关键字例子A6_LuaCall

Expand Down

0 comments on commit be9835d

Please sign in to comment.