Associated objects leaking if NSZombie objects enabled in ARC-enabled project

Originator:cto
Number:rdar://10636309 Date Originated:03-Jan-2012 06:31 PM
Status:Open Resolved:
Product:Developer Tools Product Version:4.2.1 (Build 4D502)
Classification:Serious Bug Reproducible:Always
 
03-Jan-2012 06:31 PM Sergei Cherepanov:
'AppDelegate.m' was successfully uploaded

03-Jan-2012 06:31 PM Sergei Cherepanov:
Summary:
Associated objects assigned by objc_setAssociatedObject() call are not deallocated upon deallocation of their owned, if NSZombies are enabled and project is ARC-enabled.

Steps to Reproduce:
1. Create a new Single View Application iPhone project with Storyboard and ARC enabled.
2. Replace AppDelegate.m source with the following code (I've attached a copy for your convenience):


#import "AppDelegate.h"
#import <objc/runtime.h>

static char ASSOC_KEY;

@interface DeallocTester : NSObject
@end

@implementation DeallocTester
- (id) init
{
    if (!(self = [super init]))
        return nil;
    NSLog(@"%@ inited", self);
    return self;
}

- (void) dealloc
{
    NSLog(@"%@ deallocated", self);
}
@end

@implementation AppDelegate
@synthesize window = _window;
- (void) applicationDidFinishLaunching:(UIApplication *)application
{
    NSObject *test = [[DeallocTester alloc] init];
    objc_setAssociatedObject(test, &ASSOC_KEY, [[DeallocTester alloc] init],
                             OBJC_ASSOCIATION_RETAIN);
}
@end


3. Run the project and see the following lines in Output:

2012-01-03 18:16:02.805 testD[77569:f803] <DeallocTester: 0x6abbb40> inited
2012-01-03 18:16:02.807 testD[77569:f803] <DeallocTester: 0x6ab0640> inited
2012-01-03 18:16:02.808 testD[77569:f803] <DeallocTester: 0x6abbb40> deallocated
2012-01-03 18:16:02.809 testD[77569:f803] <DeallocTester: 0x6ab0640> deallocated

4. Turn NSZombie objects on in current Scheme.
5. Run the project again

Expected Results:
You should see the same output as in step 3, 2 objects inited and both deleted.

Actual Results:
Only one object is deallocated (the associated object owner). The associated object is leaking.

2012-01-03 18:16:02.805 testD[77569:f803] <DeallocTester: 0x6abbb40> inited
2012-01-03 18:16:02.807 testD[77569:f803] <DeallocTester: 0x6ab0640> inited
2012-01-03 18:16:02.808 testD[77569:f803] <DeallocTester: 0x6abbb40> deallocated


Notes:
I'm not sure whether it is an ARC issue, I haven't tried to reproduce it without ARC. I'm also not sure whether the associated objects are actually leaking or their memory is just freed without call to -dealloc.

I've marked this issue as serious, because it blocks me from implementing block-based KVO utility NSObject category able to automatically remove KVO observers upon deallocation. My code uses an associated object to store a list of observers and removes them on deallocation. But this issue makes doing things that way unreliable, as many people have NSZombie turned on by default, and I don't know any alternative solution.

Comments

but also in ARC-disable project.


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!