Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Why does this snippet of code work?</p> </blockquote> <p>The first snipped is working because the block implementation has no reference to the surrounding scope, and it's therefore configured by clang as a <strong><em>global</em></strong> block. This causes the block not to be on the stack as it would normally be.</p> <blockquote> <p>myBlock should be destroyed.</p> </blockquote> <p>Stack frames (and their local variable) don't get <em>destroyed</em>. Their memory is made available for further allocation. Anyway, due to the simplicity of your example, you're getting a global block which doesn't live on the stack frame.</p> <p>If you were making any reference to the surrounding scope you would have had a stack-based block and <code>myBlock</code> would have been a dangling pointer to an on-owned memory location, likely leading to a crash.</p> <blockquote> <p><code>[obj retainCount]</code> in block prints 1 instead 2, why?</p> </blockquote> <p>Sounds reasonable.</p> <p>You are allocating an object (retain count: 1), the block is retaining it (retain count: 2), you are releasing it (retain count: 1), the block is eventually executed (retain count still 1).</p> <p>As a general note, anyway, don't rely upon <code>retainCount</code> when reasoning about memory. The memory management process has a lot going on under the hood, and you cannot definitely say anything only by looking at the value of <code>retainCount</code>, due to internal details of the implementation that you cannot possibly take into account. More on the subject here: <a href="http://whentouseretaincount.com/" rel="nofollow">http://whentouseretaincount.com/</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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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