title: Objective-C associated objects leaking under ARC
body: <p>I have encountered with a strange <code>objc_setAssociatedObject</code> behavior under ARC. Consider the following code:</p>
  
  <pre><code>static char ASSOC_KEY;
  
  @interface DeallocTester : NSObject
  @end
  
  @implementation DeallocTester
  - (void) dealloc
  {
      NSLog(@"DeallocTester deallocated");
      //objc_setAssociatedObject(self, &amp;ASSOC_KEY, nil, OBJC_ASSOCIATION_RETAIN);
  }
  @end
  
  @implementation AppDelegate
  - (void) applicationDidFinishLaunching:(UIApplication *)application
  {
      NSObject *test = [[DeallocTester alloc] init];
      objc_setAssociatedObject(test, &amp;ASSOC_KEY, [[DeallocTester alloc] init],
                               OBJC_ASSOCIATION_RETAIN);
  }
  </code></pre>
  
  <p>I'm creating an instance of <code>DeallocTester</code>, then I set another <code>DeallocTester</code> as an associated object for it, then both of them go out of scope.</p>
  
  <p>I expect the <code>-dealloc</code> of the first object to be called, then the associated object to be deallocated too, but I see the <code>"DeallocTester deallocated"</code> message printed only once. If I uncomment the <code>objc_setAssociatedObject</code> line in <code>-dealloc</code>, the second object gets deallocated too.</p>
  
  <p>The Objective-C reference states that associated objects are deallocated automatically upon object destruction. Is it a compiler/ARC/whatever issue or am I missing something?</p>
  
  <p><strong>Update</strong></p>
  
  <p>This sample code is actually working if you run it from a brand-new project. But I have two ARC-enabled projects where it doesn't. I'll do some investigation and provide a better sample.</p>
  
  <p><strong>Update 2</strong></p>
  
  <p>I've filled a <a href="http://openradar.appspot.com/radar?id=1472409" rel="noreferrer">rdar://10636309, Associated objects leaking if NSZombie objects enabled in ARC-enabled project</a></p>