<?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: Art Oz</title>
    <description>The latest articles on DEV Community by Art Oz (@artozf38bc119d5).</description>
    <link>https://dev.to/artozf38bc119d5</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3642734%2F60acf1a9-5a2a-4079-bd6a-2634579a3b8e.png</url>
      <title>DEV Community: Art Oz</title>
      <link>https://dev.to/artozf38bc119d5</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/artozf38bc119d5"/>
    <language>en</language>
    <item>
      <title>I Asked Claude to Create a Memory Leak in a Task and It Failed</title>
      <dc:creator>Art Oz</dc:creator>
      <pubDate>Wed, 03 Dec 2025 07:43:29 +0000</pubDate>
      <link>https://dev.to/artozf38bc119d5/i-asked-claude-to-create-a-memory-leak-in-a-task-and-it-failed-4gmg</link>
      <guid>https://dev.to/artozf38bc119d5/i-asked-claude-to-create-a-memory-leak-in-a-task-and-it-failed-4gmg</guid>
      <description>&lt;p&gt;Last week, I asked Claude to create a memory leak in Swift's Task API. It gave me code and claimed the Task captures self strongly, creating a retain cycle. It suggested using [weak self].&lt;/p&gt;

&lt;p&gt;I knew something was off. Tasks don't create retain cycles by default, that's literally their design advantage.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3l0ouah10u3weksnfhbk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3l0ouah10u3weksnfhbk.png" alt=" " width="587" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I diasgreed with him and he started to explain why I am right. The problem is that other than trusting his words I did not have any other way to prove I am right.&lt;/p&gt;

&lt;h2&gt;
  
  
  Swift Compiler
&lt;/h2&gt;

&lt;p&gt;It turns out the Swift compiler creates something called SIL (Swift Intermediate Language) - an intermediate representation that shows exactly what the compiler does with your code before turning it into machine code. &lt;br&gt;
This is the bash order you could use, preferably with “demangle” option, it’s just makes reading easier.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;bash&lt;br&gt;
swiftc -emit-sil MemoryLeak.swift | xcrun swift-demangle &amp;gt; output.sil&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; %14 = function_ref @(extension in Swift):Swift.Task&amp;lt; where B == Swift.Never&amp;gt;.init(priority: Swift.TaskPriority?, operation: __owned @isolated(any) () async -&amp;gt; A) -&amp;gt; Swift.Task&amp;lt;A, Swift.Never&amp;gt; : $@convention(method) &amp;lt;τ_0_0, τ_0_1 where τ_0_0 : Sendable, τ_0_1 == Never&amp;gt; (@in Optional&amp;lt;TaskPriority&amp;gt;, @sil_sending @owned @isolated(any) @async @callee_guaranteed @substituted &amp;lt;τ_0_0&amp;gt; () -&amp;gt; @out τ_0_0 for &amp;lt;τ_0_0&amp;gt;, @thin Task&amp;lt;τ_0_0, Never&amp;gt;.Type) -&amp;gt; @owned Task&amp;lt;τ_0_0, Never&amp;gt; // user: %15
  %15 = apply %14&amp;lt;(), Never&amp;gt;(%3, %13, %2) : $@convention(method) &amp;lt;τ_0_0, τ_0_1 where τ_0_0 : Sendable, τ_0_1 == Never&amp;gt; (@in Optional&amp;lt;TaskPriority&amp;gt;, @sil_sending @owned @isolated(any) @async @callee_guaranteed @substituted &amp;lt;τ_0_0&amp;gt; () -&amp;gt; @out τ_0_0 for &amp;lt;τ_0_0&amp;gt;, @thin Task&amp;lt;τ_0_0, Never&amp;gt;.Type) -&amp;gt; @owned Task&amp;lt;τ_0_0, Never&amp;gt; // user: %17
  dealloc_stack %3 : $*Optional&amp;lt;TaskPriority&amp;gt;     // id: %16
  release_value %15 : $Task&amp;lt;(), Never&amp;gt;            // id: %17
  %18 = tuple ()                                  // user: %19
  return %18 : $()                                // id: %19
} // end sil function 'MemoryLeak.LeakyViewController.startLeakyTask() -&amp;gt; ()'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a part we need to analyze, the whole file is about 500 lines of code. If not demangling it I could look at the code infinitely.&lt;/p&gt;

&lt;h2&gt;
  
  
  Release Value
&lt;/h2&gt;

&lt;p&gt;Lines that told the whole story:&lt;br&gt;
&lt;code&gt;release_value %15 : $Task&amp;lt;(), Never&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;By &lt;code&gt;release_value&lt;/code&gt; instruction:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Task created&lt;/li&gt;
&lt;li&gt;Task handle released immediately
&lt;/li&gt;
&lt;li&gt;Task NOT stored
No cycle created.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can read about release_value here: [(&lt;a href="https://apple-swift.readthedocs.io/en/latest/SIL.html#release-value)" rel="noopener noreferrer"&gt;https://apple-swift.readthedocs.io/en/latest/SIL.html#release-value)&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjo509c642bjidyo7mczg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjo509c642bjidyo7mczg.png" alt=" " width="800" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Analysis
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Task → ViewController 

(one-way)
ViewController → Task? NO

No cycle = No leak
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The deinit WILL be called. There is no memory leak.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Trust, but verify. Especially with AI.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>ios</category>
      <category>swift</category>
    </item>
  </channel>
</rss>
