From c878d4daa6410ab240d9de4781192c227611d94c Mon Sep 17 00:00:00 2001 From: Mygod Date: Tue, 24 May 2016 11:42:12 +0800 Subject: [PATCH] Fix DeathRecipient leaking bug --- .../shadowsocks/ServiceBoundContext.scala | 12 ++++++++-- .../com/github/shadowsocks/Shadowsocks.scala | 6 +++++ .../ShadowsocksDeathRecipient.scala | 24 ------------------- 3 files changed, 16 insertions(+), 26 deletions(-) delete mode 100644 src/main/scala/com/github/shadowsocks/ShadowsocksDeathRecipient.scala diff --git a/src/main/scala/com/github/shadowsocks/ServiceBoundContext.scala b/src/main/scala/com/github/shadowsocks/ServiceBoundContext.scala index 8096ac65..d43d3f89 100644 --- a/src/main/scala/com/github/shadowsocks/ServiceBoundContext.scala +++ b/src/main/scala/com/github/shadowsocks/ServiceBoundContext.scala @@ -9,10 +9,11 @@ import com.github.shadowsocks.ShadowsocksApplication.app /** * @author Mygod */ -trait ServiceBoundContext extends Context { +trait ServiceBoundContext extends Context with IBinder.DeathRecipient { class ShadowsocksServiceConnection extends ServiceConnection { override def onServiceConnected(name: ComponentName, service: IBinder) { - service.linkToDeath(new ShadowsocksDeathRecipient(ServiceBoundContext.this), 0) + binder = service + service.linkToDeath(ServiceBoundContext.this, 0) bgService = IShadowsocksService.Stub.asInterface(service) registerCallback ServiceBoundContext.this.onServiceConnected() @@ -21,6 +22,7 @@ trait ServiceBoundContext extends Context { unregisterCallback ServiceBoundContext.this.onServiceDisconnected() bgService = null + binder = null } } @@ -40,12 +42,14 @@ trait ServiceBoundContext extends Context { def onServiceConnected() = () def onServiceDisconnected() = () + override def binderDied = () private var callback: IShadowsocksServiceCallback.Stub = _ private var connection: ShadowsocksServiceConnection = _ private var callbackRegistered: Boolean = _ // Variables + var binder: IBinder = _ var bgService: IShadowsocksService = _ def attachService(callback: IShadowsocksServiceCallback.Stub = null) { @@ -70,6 +74,10 @@ trait ServiceBoundContext extends Context { unbindService(connection) connection = null } + if (binder != null) { + binder.unlinkToDeath(this, 0) + binder = null + } bgService = null } } diff --git a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala index bee76100..de72ce03 100644 --- a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala +++ b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala @@ -214,6 +214,12 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { if (fab != null) fab.setEnabled(false) } + override def binderDied { + detachService + crashRecovery + attachService + } + private var testCount: Int = _ private lazy val stat = findViewById(R.id.stat) private var connectionTestText: TextView = _ diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksDeathRecipient.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksDeathRecipient.scala deleted file mode 100644 index 379593da..00000000 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksDeathRecipient.scala +++ /dev/null @@ -1,24 +0,0 @@ -package com.github.shadowsocks - -import android.os.IBinder -import android.util.Log - -/** - * @author chentaov5@gmail.com - */ -class ShadowsocksDeathRecipient(val mContext: ServiceBoundContext) - extends IBinder.DeathRecipient { - - val TAG = "ShadowsocksDeathRecipient" - - override def binderDied(): Unit = { - Log.d(TAG, "[ShadowsocksDeathRecipient] binder died.") - mContext match { - case ss: Shadowsocks => - ss.detachService - ss.crashRecovery - ss.attachService - case _ => - } - } -}