title: Objective C Blocks: Is there a way to avoid 'self' being retained?
body: <p>I'm trying to write this down as concisely as possible, but it's not easy to describe -- so thanks for reading =)</p>
  
  <p>I'm the main developer of the Open Source iPhone Framework <a href="http://www.sparrow-framework.org" rel="noreferrer">Sparrow</a>. Sparrow is modeled after the Flash AS3 Library, and thus has an event system just like AS3. Currently, that system works by specifying selectors - but I would love to expand that system by allowing the use of blocks for event listeners. However, I'm stumbling over memory management issues.</p>
  
  <p>I will show you a typical use-case of events - as they are handled now.</p>
  
  <pre><code>// init-method of a display object, inheriting from 
  // the base event dispatcher class
  - (id)init
  {
      if (self = [super init])
      {
          // the method 'addEventListener...' is defined in the base class
          [self addEventListener:@selector(onAddedToStage:)
                        atObject:self
                         forType:SP_EVENT_TYPE_ADDED_TO_STAGE];
      }
      return self;
  }
  
  // the corresponding event listener
  - (void)onAddedToStage:(SPEvent *)event
  {
      [self startAnimations]; // call some method of self
  }
  </code></pre>
  
  <p>That's quite straight-forward: When an object is added to the display list, it receives an event. Currently, the base class records the event listeners in an array of NSInvocation-objects. The NSInvocation is created in a way that it <strong>does not</strong> retain its target and arguments. (The user can make it do that, but in 99% of the cases, it's not necessary).</p>
  
  <p>That these objects are not retained was a conscious choice: otherwise, the code above would cause a memory leek, even if the user removed the event listener in the dealloc-method! Here is why:</p>
  
  <pre><code>- (id)init
  {
      if (self = [super init])
      {
          // [self addEventListener: ...] would somehow cause:
          [self retain]; // (A)
      }
      return self;
  }
  
  // the corresponding event listener
  - (void)dealloc
  {
      // [self removeEventListener...] would cause:
      [self release]; // (B)
      [super dealloc];
  }
  </code></pre>
  
  <p>On first sight, that seems fine: the retain in the init-method is paired by a release in the dealloc method. However, that does not work, since the dealloc method will never be called, because the retain count never reaches zero!</p>
  
  <p>As I said, the 'addEventListener...'-method does, for exactly this reason, not retain anything in its default version. Because of the way events work (they are almost always dispatched by 'self' or child objects, which are retained anyway), that is not a problem.</p>
  
  <p>However, and now we come to the central part of the question: I cannot do that with blocks. Look at the block-variant of event handling, as I would like it to have:</p>
  
  <pre><code>- (id)init
  {
      if (self = [super init])
      {
          [self addEventListenerForType:ADDED_TO_STAGE block:^(SPEvent *event)
          {
              [self startAnimations];
          }];
      }
      return self;
  }
  </code></pre>
  
  <p>That looks great and would be very easy to use. However: when the user calls a method on 'self' or uses a member variable within the block -- which will be, well, almost always -- the block will automatically retain 'self', and the object will never be dealloc'ed.</p>
  
  <p>Now, I know that any user could rectify this by making a __block reference to self, like this:</p>
  
  <pre><code>__block id blockSelf = self;
  [self addEventListenerForType:ADDED_TO_STAGE block:^(SPEvent *event)
  {
      [blockSelf startAnimations];
  }];
  </code></pre>
  
  <p>But, honestly, I am sure almost all of the users would not know to do so or forget to do so. An API should be not only easy to use, but also <strong>hard to misuse</strong>, and this clearly violates that principle. Users of the API would most definitely misuse it.</p>
  
  <p>What bugs me is that I <strong>know</strong> that 'self' does not have to be retained -- it works in my current implementation without retaining it. So <strong>I</strong> want to tell the block that he does not need to retain self -- me, the library, should tell the block that, so that the user does not have to think about it.</p>
  
  <p>In my research, I have not found a way to do so. And I can't think of a way to change my architecture to fit that limitation of blocks.</p>
  
  <p>Has anybody got an idea what I could do about it?<br>
  Even if you have not, thanks for reading this far -- I know it was a verbose question ;-)</p>