• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Window controllers and memory leaks
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Window controllers and memory leaks


  • Subject: Window controllers and memory leaks
  • From: Jonathan Taylor <email@hidden>
  • Date: Fri, 13 Sep 2013 17:12:52 +0100

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 (email@hidden)

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:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Window controllers and memory leaks
      • From: Kyle Sluder <email@hidden>
    • Re: Window controllers and memory leaks
      • From: Dave <email@hidden>
  • Prev by Date: Re: Helper tool
  • Next by Date: Re: Helper tool
  • Previous by thread: Re: Helper tool
  • Next by thread: Re: Window controllers and memory leaks
  • Index(es):
    • Date
    • Thread