Skip to content

Commit

Permalink
fix memory leak on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
hxxft committed May 14, 2018
1 parent 7ed0f9b commit e9c750c
Show file tree
Hide file tree
Showing 23 changed files with 185 additions and 58 deletions.
8 changes: 8 additions & 0 deletions Android/example/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
<data android:scheme="lynx"/>
</intent-filter>
</activity>
<activity
android:name=".DebugActivity">
<!--<intent-filter>-->
<!--<action android:name="android.intent.action.MAIN" />-->

<!--<category android:name="android.intent.category.LAUNCHER" />-->
<!--</intent-filter>-->
</activity>
</application>

</manifest>
39 changes: 39 additions & 0 deletions Android/example/src/main/java/com/lynx/example/DebugActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.lynx.example;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

/**
* Created by dli on 2018/5/14.
*/

public class DebugActivity extends Activity {

private Button mButtom;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mButtom = new Button(this);
this.setContentView(mButtom);

mButtom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("lynx", "test");
Bundle bundle = new Bundle();
bundle.putString("page", "HomeDemo.vue");
Intent intent = new Intent(DebugActivity.this, MainActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
});
}


}
2 changes: 1 addition & 1 deletion Android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ org.gradle.jvmargs=-Xmx1536m
# org.gradle.parallel=true

# v8 or jsc
js_engine_type=v8
js_engine_type=jsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.lynx.base;

import android.content.Context;
import android.view.inputmethod.InputMethodManager;

import java.lang.reflect.Field;


public class InputMethodManagerLeakHelper {
public static void resolve(Context context) {
if (context == null) {
return;
}
try {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm == null) {
return;
}
Object obj_get = null;
Field f_mCurRootView = imm.getClass().getDeclaredField("mCurRootView");
Field f_mServedView = imm.getClass().getDeclaredField("mServedView");
Field f_mNextServedView = imm.getClass().getDeclaredField("mNextServedView");
Field f_mLastSrvView = imm.getClass().getDeclaredField("mLastSrvView");
if (f_mCurRootView.isAccessible() == false) {
f_mCurRootView.setAccessible(true);
}
obj_get = f_mCurRootView.get(imm);
if (obj_get != null) { // 不为null则置为空
f_mCurRootView.set(imm, null);
}
if (f_mServedView.isAccessible() == false) {
f_mServedView.setAccessible(true);
}
obj_get = f_mServedView.get(imm);
if (obj_get != null) { // 不为null则置为空
f_mServedView.set(imm, null);
}
if (f_mNextServedView.isAccessible() == false) {
f_mNextServedView.setAccessible(true);
}
obj_get = f_mNextServedView.get(imm);
if (obj_get != null) { // 不为null则置为空
f_mNextServedView.set(imm, null);
}

if (f_mLastSrvView.isAccessible() == false) {
f_mLastSrvView.setAccessible(true);
}
obj_get = f_mLastSrvView.get(imm);
if (obj_get != null) { // 不为null则置为空
f_mLastSrvView.set(imm, null);
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
8 changes: 8 additions & 0 deletions Android/sdk/src/main/java/com/lynx/content/LynxActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.lynx.base.InputMethodManagerLeakHelper;


public class LynxActivity extends Activity {
private LynxActivityDelegate mDelegate;

Expand All @@ -27,4 +30,9 @@ protected void onStop() {
mDelegate.onStop();
}

@Override
protected void onDestroy() {
super.onDestroy();
InputMethodManagerLeakHelper.resolve(this);
}
}
3 changes: 2 additions & 1 deletion Android/sdk/src/main/java/com/lynx/dev/DebugHostImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ public class DebugHostImpl {

public final static int DEBUG_TYPE_RELOAD = 0;

protected DebugDevHost mHost;
protected DebugDevHost mHost = null;


@CalledByNative
public void runDebug(int type) {
if(mHost == null) return;
switch (type) {
case DEBUG_TYPE_RELOAD:
mHost.debugHotReload("Debug://");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public class DevSupportManager extends DebugHostImpl {

private static DevSupportManager mDevSupportManager;
private boolean mEnable = false;

public static DevSupportManager getInstance() {
if(mDevSupportManager == null) {
Expand All @@ -14,10 +15,13 @@ public static DevSupportManager getInstance() {
}

public void setDebugDevHost(DebugDevHost host) {
mHost = host;
if(mEnable) {
mHost = host;
}
}

public void initialize() {
mEnable = true;
mDevSupportManager.nativeAttach();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public void removeExecutableAction(String sponsorAffinity, String responderAffin
return;
}
responderAffinityList.remove(responderAffinity);
CrdCommandExecutor executor = mExecutorPool.get(responderAffinity);
executor.stop();
mExecutorPool.remove(responderAffinity);
}

Expand All @@ -127,6 +129,9 @@ public void removeAllExecutableAction() {
mAffinityRelationShip.clear();
}
if (mExecutorPool != null) {
for(CrdCommandExecutor executor : mExecutorPool.values()) {
executor.stop();
}
mExecutorPool.clear();
}
}
Expand Down
6 changes: 5 additions & 1 deletion Core/base/trace_event/trace_event_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ namespace base {
class ScopedTracer {
public:
ScopedTracer() {}
~ScopedTracer() {
~ScopedTracer() {
#if ENABLE_TRACING
event_->end_timestamp_ = CurrentTimeMicroseconds();
TraceLogger::Instance()->AddTraceEvent(event_.Release());
#endif
}

void Initialize(const char* category, const char* name) {
#if ENABLE_TRACING
event_.Reset(new TraceEvent(category, name));
event_->begin_timestamp_ = CurrentTimeMicroseconds();
#endif
}

private:
Expand Down
2 changes: 1 addition & 1 deletion Core/lepus/base_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace lepus {
}

void RegisterBaseAPI(Context* ctx) {
Dictonary* table = new Dictonary;
Dictonary* table = lynx_new Dictonary;
RegisterTableFunction(ctx, table, "log", &Print);
RegisterTableFunction(ctx, table, "assert", &Assert);
RegisterFunctionTable(ctx, "console", table);
Expand Down
10 changes: 5 additions & 5 deletions Core/lepus/code_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ namespace lepus {


void CodeGenerator::EnterFunction() {
FunctionGenerate* function = new FunctionGenerate;
FunctionGenerate* function = lynx_new FunctionGenerate;
function->parent_ = current_function_;
current_function_.Reset(function);
function->function_ = new Function;
function->function_ = lynx_new Function;
FunctionGenerate* parent = function->parent_.Get();
if(parent) {
function->function_->set_index(parent->function_->AddChildFunction(function->function_));
Expand All @@ -90,7 +90,7 @@ namespace lepus {
}

void CodeGenerator::EnterBlock() {
BlockGenerate* block = new BlockGenerate;
BlockGenerate* block = lynx_new BlockGenerate;
block->parent_ = current_function_->current_block_;
current_function_->current_block_.Reset(block);
}
Expand All @@ -101,7 +101,7 @@ namespace lepus {
}

void CodeGenerator::EnterLoop() {
LoopGenerate* loop = new LoopGenerate;
LoopGenerate* loop = lynx_new LoopGenerate;
loop->loop_start_index_ = current_function_->function_->OpCodeSize();
loop->parent_ = current_function_->current_loop_;
current_function_->current_loop_.Reset(loop);
Expand Down Expand Up @@ -211,7 +211,7 @@ namespace lepus {
Guard<CodeGenerator> g(this, &CodeGenerator::EnterBlock, &CodeGenerator::LeaveBlock);
ast->block()->Accept(this, nullptr);
Value* top = context_->heap().top_++;
top->closure_ = new Closure(static_cast<Function*>(current_function_->function_));
top->closure_ = lynx_new Closure(static_cast<Function*>(current_function_->function_));
top->type_ = Value_Closure;
}
context_->root_function_.Reset(static_cast<Function*>(current_function_->function_));
Expand Down
2 changes: 1 addition & 1 deletion Core/lepus/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace lepus {
for(;iter != global_.end(); ++iter) {
iter->first->Release();
if(global_content_[iter->second].type_ == Value_Table){
delete static_cast<Dictonary*>(global_content_[iter->second].table_);
lynx_delete(static_cast<Dictonary*>(global_content_[iter->second].table_));
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Core/lepus/lepus_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ namespace lepus{
String::String(const char* str, std::size_t len, StringPool* string_pool) {
string_pool_ = string_pool;
length_ = len;
str_ = new char[len + 1];
str_ = lynx_new char[len + 1];
memcpy(str_, str, len);
str_[len] = 0;
Hash(str_);
}

String::~String(){
string_pool_->Earse(this);
delete[] str_;
lynx_deleteA(str_);
}

void String::Hash(const char *s)
Expand All @@ -36,7 +36,7 @@ namespace lepus{
if(iter != string_map_.end()) {
return iter->second;
}
String* string = new String(str.c_str(), str.size(), this);
String* string = lynx_new String(str.c_str(), str.size(), this);
string_map_.insert(std::make_pair(str, string));
return string;
}
Expand Down
2 changes: 1 addition & 1 deletion Core/lepus/math_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ namespace lepus {


void RegisterMathAPI(Context* ctx) {
Dictonary* table = new Dictonary;
Dictonary* table = lynx_new Dictonary;
RegisterTableFunction(ctx, table, "sin", &Sin);
RegisterTableFunction(ctx, table, "abs", &Abs);
RegisterTableFunction(ctx, table, "acos", &Acos);
Expand Down
Loading

0 comments on commit e9c750c

Please sign in to comment.