Okay, I know you’re supposed to use the likes of NSWindowController and 
NSViewController to load nibs, since they will automatically take care of 
releasing the top-level objects. However, for the sake of curiosity, I created 
a new project with ARC just to see what would happen with NSNib. The project 
contains a xib file named “Foo.xib” whose owner is Foo, and which contains a 
top-level custom object of class Bar. The implementations of Foo and Bar are as 
below:

@implementation Foo

- (id)init {
    if(self = [super init]) {
        NSLog(@"Foo init");
        NSNib *nib = [[NSNib alloc] initWithNibNamed:@"Foo" bundle:nil];
        NSArray *tlo = nil;
        
        [nib instantiateNibWithOwner:self topLevelObjects:&tlo];
        
        NSLog(@"top-level objects are: %@", tlo);
    }
    
    return self;
}

- (void)dealloc {
    NSLog(@"Foo dealloc");
}

@end

@implementation Bar

- (id)init {
    if(self = [super init]) {
        NSLog(@"Bar init");
    }
    
    return self;
}

- (void)dealloc {
    NSLog(@"Bar dealloc");
}

@end

The application delegate simply contains buttons to create and destroy (by 
assigning its ivar to nil) a Foo object. Clicking on both, the output looks 
like this:

2011-11-18 15:42:42.069 test[47843:707] Foo init
2011-11-18 15:42:42.075 test[47843:707] Bar init
2011-11-18 15:42:42.075 test[47843:707] top-level objects are: (
    "<Bar: 0x100534800>",
    "<NSApplication: 0x1001353f0>"
)
2011-11-18 15:42:47.437 test[47843:707] Foo dealloc

Notice that Bar never gets dealloced.

How is one supposed to manage this? You can easily get the array of the 
top-level objects, but since ARC doesn’t let you send -release to them, it 
doesn’t help much. The only ways I can think of to avoid leaking are to do 
silly things like performSelector:NSSelectorFromString(@“release”) or to wrap 
the NSNib-using code in a non-ARC source file. Neither seems ideal.

What’s the best way to deal with something like this?

Charles_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to