Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the previous answers have missed the point, or else the asker was pretty unclear. The actual question isn't talking about either array, but rather the array contents:</p> <blockquote> <p>who is responsible for releasing the <strong>objects contained in the array</strong>? Is it main() or array2?</p> </blockquote> <p><strong>Both <code>array1</code> and <code>array2</code></strong> are responsible for releasing the objects.</p> <p>From the <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/" rel="nofollow noreferrer">NSArray documentation</a>:</p> <blockquote> <p><em>"Arrays maintain strong references to their contents—in a managed memory environment, each object receives a retain message before its id is added to the array and a release message when it is removed from the array or when the array is deallocated."</em></p> </blockquote> <p>To begin with, each of the objects are retained by the NSArray <code>array1</code>. When you create <code>array2</code> via <code>-mutableCopy</code>, you get an NSMutableArray which points to the same objects, and retains each of them again. If you were to release <code>array1</code> at this point, when its <code>dealloc</code> method were called it would release each of the objects it contains. However, <code>array2</code> has retained them, so the objects won't be destroyed — only when their retain count reaches 0, which would happen if <code>array2</code> were destroyed and nobody else has retained any of the objects (or when they are removed from <code>array2</code>).</p> <p>Since collection classes (arrays, sets, dictionaries, etc.) handle retaining and releasing their contents, all you have to worry about is retaining or releasing the collection itself. Since you used <code>-mutableCopy</code>, remember that you have implicitly retained <code>array2</code>, so you should release it when you're done with it.</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. 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