<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abdullah Khan</title>
    <description>The latest articles on DEV Community by Abdullah Khan (@abdullah_khan_4a93090546d).</description>
    <link>https://dev.to/abdullah_khan_4a93090546d</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3616872%2Fd502f372-6c5a-4434-a1e1-f4c69c1ed5f6.png</url>
      <title>DEV Community: Abdullah Khan</title>
      <link>https://dev.to/abdullah_khan_4a93090546d</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abdullah_khan_4a93090546d"/>
    <language>en</language>
    <item>
      <title>Stop Leaking Coroutines: The Right Way to Make Classes Coroutine-Scoped</title>
      <dc:creator>Abdullah Khan</dc:creator>
      <pubDate>Sat, 14 Feb 2026 06:05:56 +0000</pubDate>
      <link>https://dev.to/abdullah_khan_4a93090546d/your-kotlin-class-is-probably-leaking-coroutines-52mm</link>
      <guid>https://dev.to/abdullah_khan_4a93090546d/your-kotlin-class-is-probably-leaking-coroutines-52mm</guid>
      <description>&lt;p&gt;If you create a CoroutineScope inside a class and never cancel it…&lt;/p&gt;

&lt;p&gt;you’ve already built a silent leak.&lt;/p&gt;

&lt;p&gt;It won’t crash.&lt;br&gt;
It won’t throw.&lt;br&gt;
It won’t warn you.&lt;/p&gt;

&lt;p&gt;It will just quietly keep running long after the object that started it is gone.&lt;/p&gt;

&lt;p&gt;Most developers don’t notice this — because everything “works”.&lt;/p&gt;

&lt;p&gt;Until it doesn’t.&lt;/p&gt;

&lt;p&gt;The Common Pattern That Causes It&lt;/p&gt;

&lt;p&gt;You’ve probably written something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class SimpleTask {
    val scope = CoroutineScope(Dispatchers.Default)

    fun runTask() = scope.launch {
        for (i in 0..10) {
            println(i)
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks fine, right?&lt;/p&gt;

&lt;p&gt;The object owns the scope.&lt;br&gt;
You can even add close() and cancel it manually.&lt;/p&gt;

&lt;p&gt;But here’s the real problem:&lt;/p&gt;

&lt;p&gt;Cancellation is optional.&lt;/p&gt;

&lt;p&gt;If the caller forgets to call close() —&lt;br&gt;
the coroutine keeps running.&lt;/p&gt;

&lt;p&gt;Now you’ve created what I call a runaway CoroutineScope.&lt;/p&gt;

&lt;p&gt;Why Garbage Collection Won’t Save You&lt;/p&gt;

&lt;p&gt;Many developers assume:&lt;/p&gt;

&lt;p&gt;“When the object is garbage collected, everything inside it disappears.”&lt;/p&gt;

&lt;p&gt;Not true.&lt;/p&gt;

&lt;p&gt;If your coroutine is still running, it strongly references the object.&lt;/p&gt;

&lt;p&gt;The object cannot be garbage collected.&lt;/p&gt;

&lt;p&gt;You’ve accidentally created a reference cycle.&lt;/p&gt;

&lt;p&gt;This is where most coroutine ownership discussions stop.&lt;/p&gt;

&lt;p&gt;But there is a proper architectural solution.&lt;/p&gt;

&lt;p&gt;It requires:&lt;/p&gt;

&lt;p&gt;Separating scope from owner&lt;/p&gt;

&lt;p&gt;Using WeakReference correctly&lt;/p&gt;

&lt;p&gt;Designing strict ownership boundaries&lt;/p&gt;

&lt;p&gt;Supporting both automatic and manual cancellation&lt;/p&gt;

&lt;p&gt;And it works even outside Android.&lt;/p&gt;

&lt;p&gt;I wrote a full deep dive explaining the architecture, the failure cases, and the correct implementation pattern here:&lt;/p&gt;

&lt;p&gt;👉 Read the full article:&lt;br&gt;
&lt;a href="https://medium.com/@abdullahkhan1571998/stop-leaking-coroutines-the-right-way-to-make-classes-coroutine-scoped-a45df57cbef0" rel="noopener noreferrer"&gt;Stop Leaking Coroutines: The Right Way to Make Classes Coroutine-Scoped&lt;/a&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
      <category>androiddev</category>
      <category>mobile</category>
    </item>
  </channel>
</rss>
