Note that there are some explanatory texts on larger screens.

plurals
  1. POUnderstanding the effects on retain count of methods other than retain and release
    primarykey
    data
    text
    <p>I have a routine to correctly allocate and deallocate my <code>UIView</code> subclass, but when I set a break point in the class's <code>- (void) dealloc</code>, I see that it is not called and I that have a huge memory leak.</p> <p>So I print the <code>retainCount</code> before the release and it is 2 (this could be normal, because other objects can use it). I searched and do not see any other <code>retain</code> messages. Then I send:</p> <pre><code>[myObject removeFromSuperview]; </code></pre> <p>And now it is deallocated. Which makes me realize that earlier I sent: </p> <pre><code>[self.view bringSubviewToFront:myObject]; </code></pre> <p>It is VERY DIFFICULT know that when I use <code>bringSubviewToFront:</code>, I always have to call <code>removeFromSuperview</code>. Do there exist other methods like this that can increase the retain count?</p> <p>If this object weren't a subclass, and was only a <code>UIView</code>, I would've never known that I had created a memory leak.</p> <p>So, how can I really know that my objects have been released and that the memory is free? </p> <p>It isn't important for me to know what is happening, but I want to know that I am creating and removing the object. Here's a demonstration that the same code with and without the <code>removeFromSuperview</code> can cause the effects I'm seeing -- this is my log. </p> <p>When I do <strong>not</strong> use <code>removeFromSuperview</code>,</p> <pre><code>Memory used 16687.1 ( +5378), free 262213.6 kb Memory used 19451.9 ( +2765), free 257159.2 kb Memory used 19451.9 ( +0), free 259530.8 kb Memory used 21844.0 ( +2392), free 257830.9 kb Memory used 24313.9 ( +2470), free 256356.4 kb Memory used 25260.0 ( +946), free 253141.0 kb Memory used 27848.7 ( +2589), free 252874.8 kb Memory used 30142.5 ( +2294), free 250814.5 kb Memory used 30814.2 ( +672), free 247787.5 kb </code></pre> <p>you can see the memory usage only increases, but when I <strong>do</strong> use it:</p> <pre><code>Memory used 16105.5 ( +4829), free 262619.1 kb Memory used 16527.4 ( +422), free 262815.8 kb Memory used 14168.1 ( -2359), free 262832.1 kb Memory used 16769.0 ( +2601), free 263266.3 kb Memory used 16560.1 ( -209), free 264785.9 kb Memory used 14200.8 ( -2359), free 264794.1 kb Memory used 16789.5 ( +2589), free 264290.3 kb Memory used 16597.0 ( -193), free 264499.2 kb Memory used 14237.7 ( -2359), free 264515.6 kb Memory used 16609.3 ( +2372), free 264290.3 kb Memory used 16560.1 ( -49), free 264425.5 kb Memory used 14200.8 ( -2359), free 264441.8 kb </code></pre> <p>now the memory increases and decreases like I want.</p> <p>I got the code to log the memory usage from <a href="https://stackoverflow.com/q/7990532/632472">https://stackoverflow.com/q/7990532/632472</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload