Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think you should heed the static analyser message in this case. Your pattern has potential problems.</p> <p>Specifically, when you return from the method invoked to do step 5, you are in a method of an object that may already have been deallocated. I interpret your pattern something like this:</p> <pre><code>// steps 1, 2, 3 -(void) methodThatCreatesObject { id theObj = [[TheObj alloc] init]; [theObj setDelegate: delegateObj]; // other stuff } </code></pre> <p>Note the above breaches the <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html" rel="nofollow">memory management rules</a></p> <pre><code>// step 4 - a method of theObj -(void) someMethod { [delegate notifyTaskCompleteFromObj: self]; // self points to an invalid object here. Doing anything with self results in EXC_BAD_ACCESS } </code></pre> <p>The above breaches an assumption stated in the memory management rules:</p> <blockquote> <p>A received object is normally guaranteed to remain valid within the method it was received in</p> </blockquote> <p>if we say <code>self</code> is a received object, which it is technically, since it is passed as a parameter on the stack.</p> <pre><code>// step 5 the delegate method defined in the delegate object -(void) notifyTaskCompleteFromObj: (TheObj*) anObj { // do stuff [anObj release]; } </code></pre> <p>The above also breaches the memory management rules.</p> <p>The normal pattern is to have a controller that owns both the delegate and the object that has the delegate (often the controller is itself the delegate). I think you should move to that pattern.</p>
 

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