Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 353613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:50:47+00:00 2026-05-12T11:50:47+00:00

I’m seeking further clarification after seeing What is responsible for releasing NSWindowController objects? I’m

  • 0

I’m seeking further clarification after seeing What is responsible for releasing NSWindowController objects?

I’m writing a simple inventory management application for my nephews. I have a table view that displays the contents of their “library”, etc. To add a new item to the library, they click a ‘+’ button. This button opens a new window prompting them the details of the item, and validates the input when they click ‘OK’.

All of that is working just fine. However, I have a question about the memory management. To create the new window, I use the following code:

- (IBAction)addNewItem:(id)sender {
  LibraryItemEditorController *editorController = 
    [[LibraryItemEditorController alloc] 
          initWithWindowNibName:@"LibraryItemEditor"];

  [editorController showWindow:nil];
  // editorController is "leaked" here, it seems.
}

I cannot release (nor autorelease) editorController at the end of addNewItem:, because nothing else is referencing editorController; if I release it, the window immediately disappears. I do, however, want the window controller to be released once its window is closed. In Apple’s Window Programming Guide, I read the following:

If you want the closing of a window to
make both window and window controller
go away when it isn’t part of a
document, your subclass of
NSWindowController can observe
NSWindowWillCloseNotification or, as
the window delegate, implement the
windowWillClose: method and include
the following line of code in your
implementation:

[self autorelease];

I’ve used [self autorelease] in the windowWillClose: method of the window controller. This works, and doesn’t leak memory. However, it just feels ugly; addNewItem: looks like it’s leaking memory, and static analysis thinks so too. I know that it’s actually taken care of in windowDidClose:, but it just feels wrong. Further, the window controller is now releasing itself without ever having retained itself. This all goes against the memory management rules I’ve learned.

My other option is to put an ivar on the parent controller (either an NSWindowController or an NSMutableSet of NSWindowControllers) and then watch for the NSWindowWillCloseNotification in the parent controller and release it in response. This is cleaner, and is probably what I’ll do. It’s also a fair amount more work, though, which leads me to my questions.

Is watching for the NSWindowDidCloseNotification the standard way of doing this? What’s the standard way of managing NSWindowControllers that are created and destroyed on demand? Is the [self autorelease] way the traditionally-recommended option, and it’s only now that we have static analysis that this is a problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:50:48+00:00Added an answer on May 12, 2026 at 11:50 am

    It sounds like your window is modal, in which case:

    [NSApp runModalForWindow:[editorController window]];
    [editorController release];
    

    Here’s one pattern for non-modal windows:

    @implementation QLPrefWindowController
    
    + (id) sharedInstance
    {
        if (!_sharedInstance)
        {
            _sharedInstance = [[QLPrefWindowController alloc] init];
        }
        return _sharedInstance;
    }
    
    - (void)windowWillClose:(NSNotification *)notification
    {
        if ([notification object] == [self window] && self == _sharedInstance)
        {
            _sharedInstance = nil;
            [self release];
        }
    }
    

    Then anyone who wants to access or display the window can do so through the +sharedInstance class method. If the window not is already visible, it’s created, otherwise they get the currently-visible window.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am writing an app with both english and french support. The app requests
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.