Commit c878d4da authored by Mygod's avatar Mygod

Fix DeathRecipient leaking bug

parent 9be24d74
...@@ -9,10 +9,11 @@ import com.github.shadowsocks.ShadowsocksApplication.app ...@@ -9,10 +9,11 @@ import com.github.shadowsocks.ShadowsocksApplication.app
/** /**
* @author Mygod * @author Mygod
*/ */
trait ServiceBoundContext extends Context { trait ServiceBoundContext extends Context with IBinder.DeathRecipient {
class ShadowsocksServiceConnection extends ServiceConnection { class ShadowsocksServiceConnection extends ServiceConnection {
override def onServiceConnected(name: ComponentName, service: IBinder) { 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) bgService = IShadowsocksService.Stub.asInterface(service)
registerCallback registerCallback
ServiceBoundContext.this.onServiceConnected() ServiceBoundContext.this.onServiceConnected()
...@@ -21,6 +22,7 @@ trait ServiceBoundContext extends Context { ...@@ -21,6 +22,7 @@ trait ServiceBoundContext extends Context {
unregisterCallback unregisterCallback
ServiceBoundContext.this.onServiceDisconnected() ServiceBoundContext.this.onServiceDisconnected()
bgService = null bgService = null
binder = null
} }
} }
...@@ -40,12 +42,14 @@ trait ServiceBoundContext extends Context { ...@@ -40,12 +42,14 @@ trait ServiceBoundContext extends Context {
def onServiceConnected() = () def onServiceConnected() = ()
def onServiceDisconnected() = () def onServiceDisconnected() = ()
override def binderDied = ()
private var callback: IShadowsocksServiceCallback.Stub = _ private var callback: IShadowsocksServiceCallback.Stub = _
private var connection: ShadowsocksServiceConnection = _ private var connection: ShadowsocksServiceConnection = _
private var callbackRegistered: Boolean = _ private var callbackRegistered: Boolean = _
// Variables // Variables
var binder: IBinder = _
var bgService: IShadowsocksService = _ var bgService: IShadowsocksService = _
def attachService(callback: IShadowsocksServiceCallback.Stub = null) { def attachService(callback: IShadowsocksServiceCallback.Stub = null) {
...@@ -70,6 +74,10 @@ trait ServiceBoundContext extends Context { ...@@ -70,6 +74,10 @@ trait ServiceBoundContext extends Context {
unbindService(connection) unbindService(connection)
connection = null connection = null
} }
if (binder != null) {
binder.unlinkToDeath(this, 0)
binder = null
}
bgService = null bgService = null
} }
} }
...@@ -214,6 +214,12 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { ...@@ -214,6 +214,12 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
if (fab != null) fab.setEnabled(false) if (fab != null) fab.setEnabled(false)
} }
override def binderDied {
detachService
crashRecovery
attachService
}
private var testCount: Int = _ private var testCount: Int = _
private lazy val stat = findViewById(R.id.stat) private lazy val stat = findViewById(R.id.stat)
private var connectionTestText: TextView = _ private var connectionTestText: TextView = _
......
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 _ =>
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment