Skip to content

Commit

Permalink
修复Android平台下创建类对象是内存泄漏问题
Browse files Browse the repository at this point in the history
  • Loading branch information
vimfung committed Feb 26, 2019
1 parent 9cc636d commit 8763811
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ LuaValue* LuaJavaConverter::convertToLuaValueByJObject(JNIEnv *env, LuaContext *
if (pointer != NULL)
{
value = new LuaValue(pointer);
pointer -> release();
}
else
{
Expand All @@ -382,6 +383,7 @@ LuaValue* LuaJavaConverter::convertToLuaValueByJObject(JNIEnv *env, LuaContext *
if (function != NULL)
{
value = new LuaValue(function);
function -> release();
}
else
{
Expand Down Expand Up @@ -411,6 +413,7 @@ LuaValue* LuaJavaConverter::convertToLuaValueByJObject(JNIEnv *env, LuaContext *
}

value = new LuaValue(tuple);
tuple -> release();
}
else if (env -> IsSameObject(object, NULL) != JNI_TRUE)
{
Expand Down Expand Up @@ -588,6 +591,7 @@ LuaValue* LuaJavaConverter::convertToLuaValueByJLuaValue(JNIEnv *env, LuaContext
if (pointer != NULL)
{
retValue = new LuaValue(pointer);
pointer -> release();
}
else
{
Expand All @@ -610,6 +614,7 @@ LuaValue* LuaJavaConverter::convertToLuaValueByJLuaValue(JNIEnv *env, LuaContext
if (function != NULL)
{
retValue = new LuaValue(function);
function -> release();
}
else
{
Expand Down Expand Up @@ -644,6 +649,7 @@ LuaValue* LuaJavaConverter::convertToLuaValueByJLuaValue(JNIEnv *env, LuaContext
}

retValue = new LuaValue(tuple);
tuple -> release();

env -> DeleteLocalRef(jTuple);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@ LuaObjectDescriptor* LuaJavaExportTypeDescriptor::createInstance(LuaSession *ses

LuaJavaObjectDescriptor *objectDescriptor = NULL;
LuaValue *returnValue = LuaJavaConverter::convertToLuaValueByJLuaValue(env, session -> getContext(), jReturnValue);
env -> DeleteLocalRef(jReturnValue);

if (returnValue -> getType() != LuaValueTypeNil)
{
objectDescriptor = dynamic_cast<LuaJavaObjectDescriptor *>(returnValue -> toObject());
objectDescriptor -> retain();
}
else
{
Expand All @@ -77,6 +80,8 @@ LuaObjectDescriptor* LuaJavaExportTypeDescriptor::createInstance(LuaSession *ses
value -> release();
}

returnValue -> release();

LuaJavaEnv::resetEnv(env);

return objectDescriptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ JNIEXPORT jboolean JNICALL Java_cn_vimfung_luascriptcore_LuaNativeUtil_registerT
fieldComps.clear();

env -> ReleaseStringUTFChars(fieldName, fieldNameCStr);
env -> DeleteLocalRef(fieldName);
}

//注册实例方法
Expand All @@ -388,6 +389,7 @@ JNIEXPORT jboolean JNICALL Java_cn_vimfung_luascriptcore_LuaNativeUtil_registerT
methodComps.clear();

env -> ReleaseStringUTFChars(methodName, methodNameCStr);
env -> DeleteLocalRef(methodName);
}

//注册类方法
Expand All @@ -406,6 +408,7 @@ JNIEXPORT jboolean JNICALL Java_cn_vimfung_luascriptcore_LuaNativeUtil_registerT
methodComps.clear();

env -> ReleaseStringUTFChars(methodName, methodNameCStr);
env -> DeleteLocalRef(methodName);
}

// 导出类型
Expand Down
69 changes: 33 additions & 36 deletions Source/lua-common/LuaContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,50 +278,16 @@ static void contextStartGC(LuaContext *context)
}

LuaContext::LuaContext(std::string const& platform)
: LuaContext(platform, [this]() -> lua_State* {
return LuaEngineAdapter::newState();
})
{
// _operationQueue = new LuaOperationQueue();
//
// _isActive = true;
// _exceptionHandler = NULL;
// _dataExchanger = new LuaDataExchanger(this);
//
// _operationQueue -> performAction([this]() {
//
// lua_State *state = LuaEngineAdapter::newState();
//
// LuaEngineAdapter::GC(state, LUA_GCSTOP, 0);
// //加载标准库
// LuaEngineAdapter::openLibs(state);
// LuaEngineAdapter::GC(state, LUA_GCRESTART, 0);
//
// _mainSession = new LuaSession(state, this, false);
//
// });
//
// _currentSession = NULL;
//
// //初始化类型导出管理器
// _exportsTypeManager = new LuaExportsTypeManager(this, platform);
//
// //注册错误捕获方法
// registerMethod(CatchLuaExceptionHandlerName, catchLuaExceptionHandler);
}

LuaContext::LuaContext(std::string const& platform, std::function<lua_State*(void)> const& createStateHandler)
: LuaObject()
{
_operationQueue = new LuaOperationQueue();

_isActive = true;
_exceptionHandler = NULL;
_dataExchanger = new LuaDataExchanger(this);

_operationQueue -> performAction([this, createStateHandler]() {
_operationQueue -> performAction([this]() {

lua_State *state = createStateHandler();
lua_State *state = LuaEngineAdapter::newState();

LuaEngineAdapter::GC(state, LUA_GCSTOP, 0);
//加载标准库
Expand All @@ -341,6 +307,37 @@ LuaContext::LuaContext(std::string const& platform, std::function<lua_State*(voi
registerMethod(CatchLuaExceptionHandlerName, catchLuaExceptionHandler);
}

//LuaContext::LuaContext(std::string const& platform, std::function<lua_State*(void)> const& createStateHandler)
// : LuaObject()
//{
// _operationQueue = new LuaOperationQueue();
//
// _isActive = true;
// _exceptionHandler = NULL;
// _dataExchanger = new LuaDataExchanger(this);
//
// _operationQueue -> performAction([this, createStateHandler]() {
//
// lua_State *state = createStateHandler();
//
// LuaEngineAdapter::GC(state, LUA_GCSTOP, 0);
// //加载标准库
// LuaEngineAdapter::openLibs(state);
// LuaEngineAdapter::GC(state, LUA_GCRESTART, 0);
//
// _mainSession = new LuaSession(state, this, false);
//
// });
//
// _currentSession = NULL;
//
// //初始化类型导出管理器
// _exportsTypeManager = new LuaExportsTypeManager(this, platform);
//
// //注册错误捕获方法
// registerMethod(CatchLuaExceptionHandlerName, catchLuaExceptionHandler);
//}

LuaContext::~LuaContext()
{
_isActive = false; //标记Context已经销毁
Expand Down
8 changes: 0 additions & 8 deletions Source/lua-common/LuaContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,6 @@ namespace cn
*/
LuaContext(std::string const& platform);

/**
* 初始化上下文对象
*
* @param platform 平台类型:ios,android,unity3d
* @param createStateHandler 创建状态处理器
*/
LuaContext(std::string const& platform, std::function<lua_State*(void)> const& createStateHandler);

/**
* 销毁上下文对象
*/
Expand Down

0 comments on commit 8763811

Please sign in to comment.