<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ricardo Santos on Medium]]></title>
        <description><![CDATA[Stories by Ricardo Santos on Medium]]></description>
        <link>https://medium.com/@rixantos?source=rss-9d01e5a00f56------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*u5i06szRSKN4uULBRqb30Q.jpeg</url>
            <title>Stories by Ricardo Santos on Medium</title>
            <link>https://medium.com/@rixantos?source=rss-9d01e5a00f56------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 18:35:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rixantos/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Finding Leaks in Xcode]]></title>
            <link>https://medium.com/nextfaze/finding-leaks-in-xcode-b8bc1919b112?source=rss-9d01e5a00f56------2</link>
            <guid isPermaLink="false">https://medium.com/p/b8bc1919b112</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Ricardo Santos]]></dc:creator>
            <pubDate>Fri, 10 Aug 2018 02:31:15 GMT</pubDate>
            <atom:updated>2018-08-22T00:40:38.398Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GPryEmrq5HNyXw71vyBaCg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@daanmooij">Daan Mooij</a> on <a href="https://unsplash.com/photos/91LGCVN5SAI">Unsplash</a></figcaption></figure><p>Swift and Objective C both use <a href="https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html">Automatic Reference Counting</a> (ARC) to keep track of when an object is required to persist in memory, and when it can safely be released.</p><p>Memory leaks are when an object is created and kept in memory, but is no longer owned by anything and never able to be released. Often this is a cyclic dependency where object A owns object B, and B owns object A. They will both persist in memory as they are required by each other, but nothing else. By ‘owns’, we mean ‘holds a strong reference to’.</p><p>Leaks are commonly seen in the delegate pattern. Delegate references should always be declared as weak, as a weak reference will allow the runtime to free up the memory. Thinking in a parent/child relationship, the parent should have a strong reference to the child, but the child should have a weak reference to it’s parent. In Swift, references are strong by default.</p><p>Another common place for leaks are within closures. Any variables, including self, used within a closure will be strongly captured to ensure they still exist when the closure executes. To avoid a retain cycle here, use a capture list to tell the closure to keep a weak reference.</p><p>Note that this need not apply to all closures, just those that are @escaping, which means they will be stored to be executed later. John Sundell has an excellent post on this called <a href="https://www.swiftbysundell.com/posts/capturing-objects-in-swift-closures">Capturing objects in Swift closures</a>.</p><p>Eg:</p><pre>object.run { res -&gt; Void in<br>  self.result = res <br>}</pre><p>Will leak self.</p><p>Fix this with weak or unowned as appropriate in a capture list:</p><pre>object.run { [weak self] res -&gt; Void in<br>  self?.result = res <br>}</pre><h3>Debugging Using Console Logs</h3><p>This somewhat rudimentary method involves logging. Start by placing a print statement in the init method of the class you are checking. I like to use emoji to make it easy to see in the console.</p><pre>init() { <br>  print(&quot;✅ init my class&quot;) ... <br>}</pre><p>And place another print statement in the deinit method.</p><pre>deinit {<br>  print(&quot;❌ deinit my class&quot;) <br>}</pre><p>Now run the app, and use the feature that allocates the class, then finish it so the class is deallocated (eg present a view controller, then dismiss it). You should see a balanced number of logs, eg one deinit for each init. If you don’t see the deinit log then your object is probably being leaked.</p><p>Instead of logs you could use breakpoints, and for bonus points <a href="https://twitter.com/0xced/status/900692839557992449">play a ‘pop’ sound</a> when deallocation occurs.</p><h3>Debugging Using the Memory Graph Tool</h3><p>This is a runtime tool available in Xcode when you are debugging in the simulator. To start with, edit your scheme and enable Malloc Stack.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/883/0*wsBAUd9wHprJKMaW.png" /></figure><p>Next, run your project, and exercise the app to generate leaks. For example, if you suspect a certain view controller is leaking, present/push and dismiss/pop that view controller a bunch of times. Then launch the Memory Graph Tool.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/0*gQfhKQU6-9TjcGGO.png" /></figure><p>If you have a leak, it may or may not be highlighted automatically with a purple bang, so treat this as a clue only.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*24uYUYyp_Qn9Wxz4.png" /></figure><p>In the screenshot above we can see that TimeEntryViewController has (4) next to it, indicating that there are 4 instances floating in memory. Select one to see the chain of ownership leading to the cycle. If you are really lucky, like we are above, you will even get a useful stacktrace that when clicked will navigate directly to the line of code that captures the object.</p><h3>Debugging Using Instruments</h3><p>Xcode also provides an instrument tool for detecting leaks, which is a handy tool to have on the belt.</p><p>If you want to have a play, run Xcode -&gt; Product -&gt; Profile to start Instruments, then select the Leaks template. Once it loads, press the Record button to start the app in the simulator.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/804/0*UBpFm84gTzLxQrqs.png" /></figure><p>As the app is running, use it and observe the Leak Checks row, it will be marked with a red cross when a leak is detected. Click on it to see the Stack Trace.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ApDPylJ4ddxTs4Rh.png" /></figure><h3>Conclusion</h3><p>As modern devices have significantly more RAM than the first iPhones, apps crashing due to excessive memory consumption is a rarer thing. Nonetheless, it’s always worthy of avoiding leaks, as they can often lead to other issues or bugs at runtime.</p><p>Hopefully this post will help you to plug up a few leaks in your app.</p><p><em>Originally published at </em><a href="https://nextfaze.com/blog/finding-leaks-in-xcode/"><em>www.nextfaze.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b8bc1919b112" width="1" height="1" alt=""><hr><p><a href="https://medium.com/nextfaze/finding-leaks-in-xcode-b8bc1919b112">Finding Leaks in Xcode</a> was originally published in <a href="https://medium.com/nextfaze">NextFaze</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>