Refactor GlobalScope Usage to Prevent Memory Leaks (#3396)

Replaced usage of GlobalScope with a specific coroutine scope tied to the lifecycle of the service . This change helps to prevent potential memory leaks and unintended behavior by ensuring coroutines are properly managed and tied to the appropriate lifecycle.
This commit is contained in:
Tamim Hossain authored and GitHub committed 2024-08-01 20:07:30 +08:00
1 parent 514ca0810e
commit 164412fa34
1 file changed
+3 -3
@@ -27,8 +27,8 @@ import com.v2ray.ang.util.MmkvManager
import com.v2ray.ang.util.Utils import com.v2ray.ang.util.Utils
import com.v2ray.ang.util.V2rayConfigUtil import com.v2ray.ang.util.V2rayConfigUtil
import go.Seq import go.Seq
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import libv2ray.Libv2ray import libv2ray.Libv2ray
import libv2ray.V2RayPoint import libv2ray.V2RayPoint
@@ -175,7 +175,7 @@ object V2RayServiceManager {
val service = serviceControl?.get()?.getService() ?: return val service = serviceControl?.get()?.getService() ?: return
if (v2rayPoint.isRunning) { if (v2rayPoint.isRunning) {
GlobalScope.launch(Dispatchers.Default) { CoroutineScope(Dispatchers.IO).launch {
try { try {
v2rayPoint.stopLoop() v2rayPoint.stopLoop()
} catch (e: Exception) { } catch (e: Exception) {
@@ -237,7 +237,7 @@ object V2RayServiceManager {
} }
private fun measureV2rayDelay() { private fun measureV2rayDelay() {
GlobalScope.launch(Dispatchers.IO) { CoroutineScope(Dispatchers.IO).launch {
val service = serviceControl?.get()?.getService() ?: return@launch val service = serviceControl?.get()?.getService() ?: return@launch
var time = -1L var time = -1L
var errstr = "" var errstr = ""