Skip to content

Commit

Permalink
[app] fix: 修复一个内存泄漏
Browse files Browse the repository at this point in the history
  • Loading branch information
longjunyu2 committed Jan 18, 2021
1 parent 2bb12bd commit 3159572
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
package com.aof.mcinabox.gamecontroller.input.log;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.aof.mcinabox.R;
import com.aof.mcinabox.gamecontroller.controller.Controller;
import com.aof.mcinabox.gamecontroller.definitions.manifest.AppManifest;
import com.aof.mcinabox.gamecontroller.input.Input;
Expand All @@ -31,6 +24,7 @@

import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;

import cosine.boat.LoadMe;

Expand Down Expand Up @@ -64,8 +58,8 @@ public boolean load(Context context, Controller controller) {
mLogView.setX(0);
mLogView.setY(mController.getConfig().getScreenHeight() - mLogView.getLayoutParams().height);

if (LoadMe.mReceiver == null) {
LoadMe.mReceiver = new LoadMe.LogReceiver() {
if (LoadMe.mReceiver == null || LoadMe.mReceiver.get() == null) {
LoadMe.mReceiver = new WeakReference<>(new LoadMe.LogReceiver() {
final StringBuilder stringBuilder = new StringBuilder();

@Override
Expand All @@ -79,7 +73,7 @@ public void pushLog(String log) {
public String getLogs() {
return stringBuilder.toString();
}
};
});
}

return true;
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/aof/mcinabox/launcher/uis/LogUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public int getUIVisibility() {
}

private void showLog(TextView view){
if (LoadMe.mReceiver != null && !LoadMe.mReceiver.getLogs().equals("")) {
view.setText(LoadMe.mReceiver.getLogs());
if (LoadMe.mReceiver != null && LoadMe.mReceiver.get() != null && !LoadMe.mReceiver.get().getLogs().equals("")) {
view.setText(LoadMe.mReceiver.get().getLogs());
} else {
try {
view.setText(FileTool.readToString(AppManifest.BOAT_LOG_FILE));
Expand Down
12 changes: 7 additions & 5 deletions boat/src/main/java/cosine/boat/LoadMe.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import android.system.Os;
import android.util.Log;

import java.lang.ref.WeakReference;
import java.util.Map;

public class LoadMe {

private final static String TAG = "LoadMe";

public static LogReceiver mReceiver;
public static WeakReference<LogReceiver> mReceiver;

public static native int chdir(String str);

Expand Down Expand Up @@ -73,9 +74,9 @@ public void run() {
}

public static void receiveLog(String str){
if (mReceiver == null) {
if (mReceiver == null || mReceiver.get() == null) {
Log.e(TAG, "LogReceiver is null. So use default receiver.");
mReceiver = new LogReceiver() {
mReceiver = new WeakReference<>(new LogReceiver() {
final StringBuilder builder = new StringBuilder();
@Override
public void pushLog(String log) {
Expand All @@ -87,9 +88,10 @@ public void pushLog(String log) {
public String getLogs() {
return builder.toString();
}
};
});
} else {
mReceiver.get().pushLog(str);
}
mReceiver.pushLog(str);
}

static {
Expand Down

0 comments on commit 3159572

Please sign in to comment.