Skip to content

Commit

Permalink
1. 修复Android平台下table转换到原生层后发生内存泄漏问题
Browse files Browse the repository at this point in the history
2. 调整底层table转换问题,取消LuaTable类,修复Android平台下由于LuaValue的finalize方法不调用情况下产生的内存泄漏。
  • Loading branch information
vimfung committed Jul 13, 2019
1 parent 8932a85 commit f5e46d3
Show file tree
Hide file tree
Showing 22 changed files with 453 additions and 610 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ LuaValue constructorMethodRoute(LuaContext context, Class<? extends LuaExportTy
try
{
Object instance = constructor.newInstance(args.toArray(new Object[0]));

return new LuaValue(instance);
}
catch (Exception e)
Expand Down Expand Up @@ -483,7 +484,6 @@ LuaValue getterMethodRoute(LuaContext context, Object instance, String fieldName
Class type = instance.getClass();
if (_regFieldMethods.containsKey(type) && _regFieldMethods.get(type).containsKey(fieldName))
{
//将LuaValue数组转换为对象数组
Field field = _regFieldMethods.get(type).get(fieldName);
if (field == null)
{
Expand Down Expand Up @@ -517,8 +517,6 @@ void setterMethodRoute(LuaContext context, Object instance, String fieldName, Lu
Class type = instance.getClass();
if (_regFieldMethods.containsKey(type) && _regFieldMethods.get(type).containsKey(fieldName))
{
//将LuaValue数组转换为对象数组
ArrayList argumentArray = new ArrayList();
Field field = _regFieldMethods.get(type).get(fieldName);
if (field == null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cn.vimfung.luascriptcore;

import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -14,19 +16,7 @@ public class LuaValue extends LuaBaseObject
private Object _valueContainer;
private LuaValueType _type;
private LuaContext _context; //从JNI层回来的对象会带上这个字段
private int _tableId; //Map和Array类型下的Table标识

@Override
protected void finalize() throws Throwable
{
if (_tableId > 0)
{
//释放table对象
LuaNativeUtil.releaseNativeObject(_tableId);
}

super.finalize();
}
private String _tableId; //Map和Array类型下的Table标识

/**
* 初始化一个空值的LuaValue对象
Expand Down Expand Up @@ -257,10 +247,11 @@ public LuaValue (List<?> value)
* @param nativeId 本地对象标识
* @param value 数组
*/
protected LuaValue (int nativeId, List<?> value)
protected LuaValue (int nativeId, List<?> value, String tableId)
{
super(nativeId);
setArrayListValue(value);
_tableId = tableId;
}

/**
Expand Down Expand Up @@ -288,10 +279,11 @@ public LuaValue (Map<?, ?> value)
* @param nativeId 本地对象标识
* @param value 哈希表
*/
protected LuaValue (int nativeId, Map<?, ?> value)
protected LuaValue (int nativeId, Map<?, ?> value, String tableId)
{
super(nativeId);
setHasMapValue(value);
_tableId = tableId;
}

/**
Expand Down Expand Up @@ -757,4 +749,13 @@ public void setObject(String keyPath, Object object)
_valueContainer = LuaNativeUtil.luaValueSetObject(_context, this, keyPath, new LuaValue(object));
}
}

/**
* 获取table标识
* @return table标识
*/
protected String getTableId()
{
return _tableId;
}
}
1 change: 0 additions & 1 deletion Source/Android/luascriptcore/src/main/jni/Android-5.1.5.mk
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ LOCAL_SRC_FILES := \
../../../../../lua-common/LuaCoroutine.cpp \
../../../../../lua-common/LuaError.cpp \
../../../../../lua-common/LuaScriptController.cpp \
../../../../../lua-common/LuaTable.cpp \

LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../../../../lua-core-5.1.5/src
Expand Down
1 change: 0 additions & 1 deletion Source/Android/luascriptcore/src/main/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ LOCAL_SRC_FILES := \
../../../../../lua-common/LuaCoroutine.cpp \
../../../../../lua-common/LuaError.cpp \
../../../../../lua-common/LuaScriptController.cpp \
../../../../../lua-common/LuaTable.cpp \

LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../../../../lua-core/src
Expand Down
3 changes: 1 addition & 2 deletions Source/Android/luascriptcore/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ add_library( # Sets the name of the library.
../../../../../lua-common/LuaExportTypeDescriptor.cpp
../../../../../lua-common/LuaExportPropertyDescriptor.cpp
../../../../../lua-common/LuaError.cpp
../../../../../lua-common/LuaScriptController.cpp
../../../../../lua-common/LuaTable.cpp)
../../../../../lua-common/LuaScriptController.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included in the search path by
Expand Down
Loading

0 comments on commit f5e46d3

Please sign in to comment.