makeKeyAndOrderFront barf on second use ...(ARC?) [英] makeKeyAndOrderFront barfs on second use... (ARC?)

查看:523
本文介绍了makeKeyAndOrderFront barf on second use ...(ARC?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题...



我以前使用MainMenu.xib中的代码来管理窗口:



-

  if([olt_MainMenu_WINDOW isVisible]){
[olt_MainMenu_WINDOW setReleasedWhenClosed:NO];
[olt_MainMenu_WINDOW close];
}

...然后

  if(olt_DoSomething_WINDOW == nil)
if(![[NSBundle mainBundle] loadNibNamed:@DoSomethingowner:self topLevelObjects:nil])
NSBeep();
if(olt_DoSomething_WINDOW!= nil)
[olt_DoSomething_WINDOW makeKeyAndOrderFront:nil];

-



...在MainMenu.xib中回到我的窗口:



-

  if([olt_DoSomething_WINDOW isVisible]){
[olt_DoSomething_WINDOW setReleasedWhenClosed:NO];
[olt_DoSomething_WINDOW close];
}

...然后

  if(olt_MainMenu_WINDOW == nil)
if(![[NSBundle mainBundle] loadNibNamed:@MainMenuowner:self topLevelObjects:nil])
NSBeep();
if(olt_MainMenu_WINDOW!= nil)
[olt_MainMenu_WINDOW makeKeyAndOrderFront:nil];

-



to makeKeyAndOrderFront doSomething窗口第二次,它barfs与exc_bad_access(代码= 1 ...



我可以makeKeyAndOrderFront MainMenu多次,但没有任何其他。我缺少了?



是的,在关闭时释放没有在窗口的属性检查器中选择,这是我第一次使用ARC,


解决方案

基于 的参考资料loadNibNamed:owner:topLevelObjects: ,我想你有一个弱的 IBOutlet & myArray 而是使用的顶层对象, topLevelObjects:nil ,很清楚,有必要通过使用IBOutlets来强有力地引用它们。


讨论



与传统方法不同,对象[ topLevelObjects- ed。]遵守标准可可内存管理规则;有必要通过使用IBOutlet或保存对数组的引用来保持对它们的强引用,以防止nib内容被重新分配。



到顶级对象的出口应该是强烈的参考,以证明所有权和防止重新分配。


您需要:



@property强)IBOutlet NSWindow * olt_DoSomething_WINDOW;



...否则保持对窗口的强引用。 通常是弱的,因为他们的视图元素保留他们的superview你不需要控制器保留,但你的 NSWindow 这里没有保留所有者,除非你明确创建一个。所以这将是一个你真正想要一个强大的IBOutlet的情况。


Question...

I’ve previously used this code from my MainMenu.xib to manage windows:

--

if([olt_MainMenu_WINDOW isVisible]){
    [olt_MainMenu_WINDOW setReleasedWhenClosed:NO];
    [olt_MainMenu_WINDOW close];
}

...and then

if(olt_DoSomething_WINDOW == nil)
    if(![[NSBundle mainBundle] loadNibNamed:@"DoSomething" owner:self topLevelObjects:nil])
        NSBeep();
if(olt_DoSomething_WINDOW != nil)
    [olt_DoSomething_WINDOW makeKeyAndOrderFront:nil];

--

...then go back to my window in my MainMenu.xib:

--

if([olt_DoSomething_WINDOW isVisible]){
    [olt_DoSomething_WINDOW setReleasedWhenClosed:NO];
    [olt_DoSomething_WINDOW close];
}

...and then

if(olt_MainMenu_WINDOW == nil)
    if(![[NSBundle mainBundle] loadNibNamed:@"MainMenu" owner:self topLevelObjects:nil])
        NSBeep();
if(olt_MainMenu_WINDOW != nil)
    [olt_MainMenu_WINDOW makeKeyAndOrderFront:nil];

--

but if I try to makeKeyAndOrderFront the DoSomething window a second time, it barfs with exc_bad_access (code=1 ...

I can makeKeyAndOrderFront the MainMenu more than once but not any others. What am I missing?

Yes, 'Release When Closed’ is not selected in the Attributes inspector for the window. This is the first time I've used this with ARC, could it be related to that?

解决方案

Based on the reference for loadNibNamed:owner:topLevelObjects:, I'm going to guess that you have a weak IBOutlet to the NSWindow top level object in the doSomething nib. Since you're not passing something like &myArray and instead using topLevelObjects:nil, it's pretty clear that "it is necessary to keep a strong reference to them by using IBOutlets."

Discussion

Unlike legacy methods, the objects [topLevelObjects-ed.] adhere to the standard cocoa memory management rules; it is necessary to keep a strong reference to them by using IBOutlets or holding a reference to the array to prevent the nib contents from being deallocated.

Outlets to top-level objects should be strong references to demonstrate ownership and prevent deallocation.

You need:

@property (strong) IBOutlet NSWindow *olt_DoSomething_WINDOW;

... or to otherwise keep a strong reference to the window. IBOutlets are generally weak because they are view elements retained by their superview that you do not need the controller retaining, but your NSWindow here has no retaining owner unless you explicitly create one. So it would be a case where you actually want a strong IBOutlet.

这篇关于makeKeyAndOrderFront barf on second use ...(ARC?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆