Skip to content

Commit

Permalink
Fix memory leak for android platform.
Browse files Browse the repository at this point in the history
  • Loading branch information
dumganhar committed Sep 4, 2024
1 parent 9333b48 commit 75597cd
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,22 @@ protected void onDestroy() {
CocosHelper.unregisterBatteryLevelReceiver(this);
CocosAudioFocusManager.unregisterAudioFocusListener(this);
CanvasRenderingContext2DImpl.destroy();
CocosHelper.destroy();
GlobalObject.destroy();
CocosWebViewHelper.resetStaticVariables();
CocosSensorHandler.resetStaticVariables();

mVideoHelper.destroy();
mSurfaceView.setOnTouchListener(null);
mSurfaceView.getHolder().removeCallback(this);

mRootLayout.removeAllViews();
mRootLayout = null;

mSensorHandler = null;
mWebViewHelper = null;
mVideoHelper = null;
mSurfaceView = null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class CocosHelper {
// ===========================================================

private static Vibrator sVibrateService;
private static BatteryReceiver sBatteryReceiver = new BatteryReceiver();
private static BatteryReceiver sBatteryReceiver = null;

public static final int NETWORK_TYPE_NONE = 0;
public static final int NETWORK_TYPE_LAN = 1;
Expand All @@ -98,6 +98,13 @@ public void addTask(Runnable runnable) {
sTaskQ.add(runnable);
}
}

public void clearTasks() {
synchronized (readMtx) {
sTaskQ.clear();
}
}

public void runTasks(){
Queue<Runnable> tmp;
synchronized (readMtx) {
Expand Down Expand Up @@ -135,12 +142,18 @@ public void setBatteryLevelByIntent(Intent intent) {
}

static void registerBatteryLevelReceiver(Context context) {
if (sBatteryReceiver == null) {
sBatteryReceiver = new BatteryReceiver();
}
Intent intent = context.registerReceiver(sBatteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
sBatteryReceiver.setBatteryLevelByIntent(intent);
}

static void unregisterBatteryLevelReceiver(Context context) {
context.unregisterReceiver(sBatteryReceiver);
if (sBatteryReceiver != null) {
context.unregisterReceiver(sBatteryReceiver);
sBatteryReceiver = null;
}
}

//Run on game thread forever, no matter foreground or background
Expand Down Expand Up @@ -197,6 +210,13 @@ public static void init() {
}
}

public static void destroy() {
sVibrateService = null;
sInited = false;
sTaskQOnGameThread.clearTasks();
sForegroundTaskQOnGameThread.clearTasks();
}

public static float getBatteryLevel() {
return sBatteryReceiver.sBatteryLevel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ public static boolean init(String dbName, String tableName) {
}

public static void destroy() {
mDatabaseOpenHelper = null;
if (mDatabase != null) {
mDatabase.close();
mDatabase = null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public CocosSensorHandler(final Context context) {
mSensorHandler = this;
}

public static void resetStaticVariables() {
mSensorHandler = null;
mEnableSensor = false;
}

// ===========================================================
// Getter & Setter
// ===========================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public class CocosVideoHelper {
private Activity mActivity = null;
private static SparseArray<CocosVideoView> sVideoViews = null;
static VideoHandler mVideoHandler = null;
private static Handler sHandler = null;

CocosVideoHelper(Activity activity, FrameLayout layout)
{
Expand All @@ -57,7 +56,16 @@ public class CocosVideoHelper {

mVideoHandler = new VideoHandler(this);
sVideoViews = new SparseArray<CocosVideoView>();
sHandler = new Handler(Looper.myLooper());
}

public void destroy() {
if (mVideoHandler != null) {
mVideoHandler.removeCallbacksAndMessages(null);
mVideoHandler = null;
}
videoEventListener = null;
mLayout = null;
mActivity = null;
}

private static int videoTag = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public CocosWebViewHelper(FrameLayout layout) {
CocosWebViewHelper.webViews = new SparseArray<CocosWebView>();
}

public static void resetStaticVariables() {
sLayout = null;
if (sHandler != null) {
sHandler.removeCallbacksAndMessages(null);
sHandler = null;
}
webViews = null;
}

private static native boolean shouldStartLoading(int index, String message);
private static native void didFinishLoading(int index, String message);
private static native void didFailLoading(int index, String message);
Expand Down

0 comments on commit 75597cd

Please sign in to comment.