Weird issue with NSWindowController and ARC

Deadly 提交于 2019-12-24 08:57:41

问题


OK, here's a weird situation I'm facing :

  • I've got an NSWindowController subclass, a window in myMainWindow.xib and the AppDelegate.m invoking the NSWindowController like this :

    myWindowController* controller = [[myWindowController alloc] initWithWindowNibName:@"myMainWindow"];
    [controller showWindow:self];
    

Now here's what :

  • When ARC is set to NO, then the app runs fine and the windows shows up ok, too.
  • When I set ARC to YES - immediately after I set it (and for JUST 1 run) - it shows a bunch of errors :

Feb 17 16:35:45 DrKameleons-iMac.local MyApp[16903] : kCGErrorIllegalArgument: _CGSFindSharedWindow: WID 2409 Feb 17 16:35:45 DrKameleons-iMac.local MyApp[16903] : kCGErrorFailure: Set a breakpoint @ CGErrorBreakpoint() to catch errors as they are logged. Feb 17 16:35:45 DrKameleons-iMac.local MyApp[16903] : kCGErrorIllegalArgument: CGSOrderFrontConditionally: Invalid window

And the window does not show up.

Now if I try rerunning the app (without changing anything, ARC is still ON) the window still doesn't show up. But there is no error at all. :S

What is going on? Any ideas on how to fix this (and make the window appear)?


HINTS :

  • Running on Mac OS X 10.7.5
  • Xcode version : 4.5.1

回答1:


when ARC is on, the LOCAL controller is released right after its showWindow call! but as windows are deferred, the window isn't even there yet -- and even it were, it would be 'controller-less'
the Window does NOT retain its controller :) only vice-versa

the fix is to remember a reference to the controller Until your appDelegate is deallocated/until you quit

@implementation AppDelegate {
    myWindowController* _controller;
}

...
_controller = [[myWindowController alloc] initWithWindowNibName:@"myMainWindow"];
[_controller showWindow:self];

your code works without arc because no release of controller happens and it just leaks! ARC does add a release call though (as it correctly sees controller is no longer used)



来源:https://stackoverflow.com/questions/14922249/weird-issue-with-nswindowcontroller-and-arc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!