Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The correct behavior depends on the declaration of the editMyObject @property. Assuming it is delcared as</p> <pre><code>@property (retain) id editMyObject; //id may be replaced by a more specific type </code></pre> <p>or </p> <pre><code>@property (copy) id editMyObject; </code></pre> <p>then assignment via <code>self.editMyObject =</code> retains or copies the assigned object. Since <code>[[MyObject alloc] init]</code> returns a retained object, that you as the caller own, you have an extra retain of the MyObject instance and it will therefore leak unless it there is a matching release (as in the second block). I would suggest you read the <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html" rel="nofollow noreferrer">Memory Management Programming Guide</a>[2].</p> <p>Your second code block is correct, assuming the property is declared as described above.</p> <p>p.s. You should not use <code>[self.editMyObject release]</code> in a <code>-dealloc</code> method. You should call <code>[editMyObject release]</code> (assuming the ivar backing the @property is called <code>editMyObject</code>). Calling the accessor (via <code>self.editMyObject</code> is safe for @synthesized accessors, but if an overriden accessor relies on object state (which may not be valid at the calling location in <code>-dealloc</code> or causes other side-effects, you have a bug by calling the accessor.</p> <p>[2] Object ownership rules in Cocoa are very simple: if you call a method that has <code>alloc</code>, or <code>copy</code> in its signature (or use <code>+[NSObject new]</code> which is basically equivalent to <code>[[NSObject alloc] init]</code>), then you "own" the object that is returned and you must balance your acquisition of ownership with a <code>release</code>. In all other cases, you do not own the object returned from a method. If you want to keep it, you must take ownership with a <code>retain</code>, and later release ownership with a <code>release</code>.</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