High: Coroutine leaks, unsafe InputStream, and cache never invalidated #27

Closed
opened 2026-06-10 15:16:01 +00:00 by Mark · 0 comments
Owner

Findings

  • H1: CoroutineScope(Dispatchers.Main) created in ColoringActivity, GalleryActivity, and StoryActivity but never cancelled in onDestroy(). Coroutines outlive the Activity, crash with "View not attached to window manager", and leak the entire Activity context. Fix: use lifecycleScope or cancel in onDestroy().

  • H2: StoryRepository.kt reads assets using InputStream.available() which is not guaranteed to return total file size (returns 0 for compressed assets). read(buffer) may read fewer bytes than the buffer size. Can produce truncated JSON. Fix: use bufferedReader().readText().

  • H3: cachedStories and cachedChapters never invalidated. If story data changes (downloaded update), app needs process restart.

Affected files

  • android/app/src/main/java/com/storyplay/activitybook/ui/ColoringActivity.kt
  • android/app/src/main/java/com/storyplay/activitybook/ui/GalleryActivity.kt
  • android/app/src/main/java/com/storyplay/activitybook/ui/StoryActivity.kt
  • android/app/src/main/java/com/storyplay/activitybook/data/StoryRepository.kt
## Findings - **H1**: `CoroutineScope(Dispatchers.Main)` created in ColoringActivity, GalleryActivity, and StoryActivity but **never cancelled** in `onDestroy()`. Coroutines outlive the Activity, crash with "View not attached to window manager", and leak the entire Activity context. Fix: use `lifecycleScope` or cancel in `onDestroy()`. - **H2**: `StoryRepository.kt` reads assets using `InputStream.available()` which is not guaranteed to return total file size (returns 0 for compressed assets). `read(buffer)` may read fewer bytes than the buffer size. Can produce truncated JSON. Fix: use `bufferedReader().readText()`. - **H3**: `cachedStories` and `cachedChapters` never invalidated. If story data changes (downloaded update), app needs process restart. ## Affected files - `android/app/src/main/java/com/storyplay/activitybook/ui/ColoringActivity.kt` - `android/app/src/main/java/com/storyplay/activitybook/ui/GalleryActivity.kt` - `android/app/src/main/java/com/storyplay/activitybook/ui/StoryActivity.kt` - `android/app/src/main/java/com/storyplay/activitybook/data/StoryRepository.kt`
Sign in to join this conversation.
No description provided.