2

I got an NSDocument-based app. In the document class, a window is created. This window controller is added in -makeWindowControllers (code below). If I check out the retain count of my controller ([_windowController retainCount]) right after adding it, the retain count equals 3. But why? I'd imagine it should be two? What happens in -addWindowController:?

 - (void)makeWindowControllers {

    NSLog(@"%li", (unsigned long)[[self windowController] retainCount]); // Outputs 1
    [self addWindowController:[self windowController]];
    NSLog(@"%li", (unsigned long)[_windowController retainCount]); // Outputs 3

}

- (ProjectWindowController *)windowController {

    if (!_windowController) { // Lazy loading
        _windowController = [ProjectWindowControllerYosemite new];
    }

    return _windowController;

}
  • `addWindowController` "Adds the specified window controller to this document’s list of attached window controllers and sets the document of the passed-in window controller.". Is the retainCount of 3 a problem? – Willeke Nov 09 '17 at 16:14
  • 1
    Why does the retain count increase by 2? It causes problems in that, when I'm releasing the window controller upon termination, its `-dealloc` method never gets called. – Bruno Vandekerkhove Nov 09 '17 at 17:16
  • 1
    Does the retainCount decrease by 2 when you call `removeWindowController`? – Willeke Nov 10 '17 at 03:46
  • Made a test project, and that one ups the retain count by one, not by two. There must be something silly I'm overlooking in my code. – Bruno Vandekerkhove Nov 10 '17 at 16:33
  • Turns out to be due to an `NSTimer` instance created in my custom window controller ; it keeps a strong reference to the controller (which is its target). I traced the additional retain by using the code [in this answer](https://stackoverflow.com/a/5587553/3576399). – Bruno Vandekerkhove Nov 10 '17 at 16:54
  • The retain count is useless. Ignore it. It'll never give you a definitive answer about anything. In this case, it is an implementation detail. – bbum Nov 27 '17 at 19:10

0 Answers0