fix: Task 4 완료 - LunaticLauncher Toast 및 LauncherActivity onDestroy 메모리 누수 수정

- LunaticLauncher.kt: companion object의 정적 Toast 강참조(var toast: Toast?)로 인한 Activity Context 누수 제거 -> applicationContext 기반 WeakReference<Toast>로 전환 및 이전 Toast 취소 처리 유지
- LauncherActivity.kt onDestroy():
  * sActivity === this 조건으로 정적 Activity 참조 null 해제
  * hiddenLoader 내 모든 GeckoWeb 세션(session?.close(), releaseSession()) 종료 및 clear() 처리
  * callBackHandler.removeCallbacksAndMessages(null) 호출로 대기 중인 콜백 제거
  * connectivityManager.unregisterNetworkCallback 호출 시 ::connectivityManager.isInitialized 및 예외 방어 추가
- TODO.md: 1-2 항목 완료 처리
- 검증: ./gradlew :app:compileDebugKotlin 빌드 성공 확인 (6s)
This commit is contained in:
2026-11-16 11:12:16 +09:00
parent 244d3cee14
commit 8c278c2fc4
3 changed files with 29 additions and 10 deletions
+1 -1
View File
@@ -43,7 +43,7 @@
- `app/src/main/kotlin/bums/lunatic/launcher/home/RssHome.kt:95` — `var home: RssHome? = null` 정적 참조로 Fragment 및 연결된 View/Binding 누수 위험. - `app/src/main/kotlin/bums/lunatic/launcher/home/RssHome.kt:95` — `var home: RssHome? = null` 정적 참조로 Fragment 및 연결된 View/Binding 누수 위험.
- **작업**: 정적 Activity/Fragment 참조를 `WeakReference` 또는 이벤트 버스(`SharedFlow`/`ViewModel`)로 대체하고, 최소한 `onDestroy()`/`onDestroyView()`에서 `null` 처리. - **작업**: 정적 Activity/Fragment 참조를 `WeakReference` 또는 이벤트 버스(`SharedFlow`/`ViewModel`)로 대체하고, 최소한 `onDestroy()`/`onDestroyView()`에서 `null` 처리.
- [ ] **1-2. 전역 `Toast` 인스턴스의 `Context` 누수 해결** - [x] **1-2. 전역 `Toast` 인스턴스의 `Context` 누수 해결**
- `app/src/main/kotlin/bums/lunatic/launcher/LunaticLauncher.kt:62-75` — `companion object`의 `var toast: Toast? = null`에 `Context.toast()` 호출 시 전달된 `Context`(Activity일 수 있음)로 `Toast(this)`를 생성해 정적으로 유지함. - `app/src/main/kotlin/bums/lunatic/launcher/LunaticLauncher.kt:62-75` — `companion object`의 `var toast: Toast? = null`에 `Context.toast()` 호출 시 전달된 `Context`(Activity일 수 있음)로 `Toast(this)`를 생성해 정적으로 유지함.
- **작업**: `Toast(applicationContext)`를 사용하거나 매 호출 시 지역 `Toast`를 생성하고 기존 `toast?.cancel()` 후 참조를 유지하지 않도록 변경. - **작업**: `Toast(applicationContext)`를 사용하거나 매 호출 시 지역 `Toast`를 생성하고 기존 `toast?.cancel()` 후 참조를 유지하지 않도록 변경.
@@ -976,7 +976,25 @@ open class LauncherActivity : CommonActivity() {
// smsReceiver = null // smsReceiver = null
// } // }
networkCallback?.let { connectivityManager.unregisterNetworkCallback(it) } if (::connectivityManager.isInitialized) {
networkCallback?.let {
try {
connectivityManager.unregisterNetworkCallback(it)
} catch (_: Exception) { }
}
}
networkCallback = null
hiddenLoader.values.forEach { gecko ->
try {
gecko.session?.close()
gecko.releaseSession()
} catch (_: Exception) { }
}
hiddenLoader.clear()
callBackHandler.removeCallbacksAndMessages(null)
if (sActivity === this) {
sActivity = null
}
super.onDestroy() super.onDestroy()
} }
@@ -59,19 +59,20 @@ internal class LunaticLauncher : Application() {
} }
var privateCompletedDir : File? = null var privateCompletedDir : File? = null
var toast : Toast? = null private var toastRef: java.lang.ref.WeakReference<Toast>? = null
fun Context.toast(msg: String) { fun Context.toast(msg: String) {
val biggerText = SpannableStringBuilder(msg) val biggerText = SpannableStringBuilder(msg)
biggerText.setSpan(RelativeSizeSpan(1.6f), 0, msg.length, 0) biggerText.setSpan(RelativeSizeSpan(1.6f), 0, msg.length, 0)
val view: View = inflate(this, R.layout.simple_toast, null) val appCtx = applicationContext ?: this
val view: View = inflate(appCtx, R.layout.simple_toast, null)
view.findViewById<TextView>(R.id.text).text = biggerText view.findViewById<TextView>(R.id.text).text = biggerText
if (toast==null) { toastRef?.get()?.cancel()
toast = Toast(this) Toast(appCtx).apply {
toast?.duration = Toast.LENGTH_SHORT duration = Toast.LENGTH_SHORT
toast?.cancel() this.view = view
toastRef = java.lang.ref.WeakReference(this)
show()
} }
toast?.view = view
toast?.show()
} }
} }