This must be an incredibly basic question, but I haven't found an answer I'm convinced by (apologies if I have missed something on the list). My question relates to window controllers, and how ownership, retain/release etc should be managed in order to (a) be correct and (b) satisfy the static analyzer. This has come up because it's only now that I have migrated my codebase to be compatible with the latest version of xcode that I have been able to run the static analyzer over it and examine the results.
I want to allocate a window + controller, and I want it to live until the user closes the GUI window, at which point I want it to disappear and clean up after itself. I believe that the following code does not leak memory and behaves as intended. @interface MyWindowController : NSWindowController <NSWindowDelegate> { } @end @implementation MyWindowController -(id)init { if (!(self = [self initWithWindowNibName:@"MyNib"])) return nil; // Window will release self when closed, so we need to retain an extra time here [self retain]; return self; } -(void)dealloc { printf("Deallocated\n"); [super dealloc]; } -(void)windowWillClose:(NSNotification*)note { [self autorelease]; } @end void TestWindowController(void) { MyWindowController *myWindowController = [[MyWindowController alloc] init]; [myWindowController.window makeKeyAndOrderFront:nil]; // We own a reference to myWindow since we allocated it, // but we have now finished all the setup we want to do // and are relinquishing control of the window object, // releasing it into the big wide world to live or die // as it may. [myWindowController release]; } However the static analyzer complains that there is a "potential leak" of myWindowController, because it recognises that it has a retain count of 2 when it returns from the init method. (The same applies if I don't retain in init and don't release in TestWindowController). It strikes me that this would be quite a common pattern. I appreciate that the static analyzer doesn't *know* whether there's a leak or not, but if I am indeed correctly following a common pattern then I would have expected the analyzer to understand what is going on. My question then is whether I am doing things in an unconventional way here, and/or whether there is something I could change that would help the analyzer understand what is going on. Many thanks Jonny. _______________________________________________ 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com