Objective c 关闭模式窗口时的EXC_错误访问([NSWindow_restoreLevelAfterRunningModal]:发送到解除分配实例的消息)

Objective c 关闭模式窗口时的EXC_错误访问([NSWindow_restoreLevelAfterRunningModal]:发送到解除分配实例的消息),objective-c,automatic-ref-counting,modal-dialog,exc-bad-access,Objective C,Automatic Ref Counting,Modal Dialog,Exc Bad Access,在我的主ViewController中,我有以下代码: - (IBAction)listFunctions:(id)sender //button clicked { FunctionListController *functionListController = [[FunctionListController alloc] initWithWindowNibName:@"FunctionList"]; NSWindow *functionListWindow = [func

在我的主ViewController中,我有以下代码:

- (IBAction)listFunctions:(id)sender //button clicked
{
    FunctionListController *functionListController = [[FunctionListController alloc] initWithWindowNibName:@"FunctionList"];

    NSWindow *functionListWindow = [functionListController window];

    [NSApp runModalForWindow: functionListWindow];

    NSLog(@"done");
}
FunctionListController
FunctionList.nib
的文件所有者,是
NSWindowController
的子类,实现协议
NSWindowDelegate

下面是
FunctionListController
的实现:

@implementation FunctionListController

- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if(self)
    {
        // Initialization code here.
    }

    return self;
}

- (void)windowDidLoad
{
    [super windowDidLoad];

    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
    self.window.delegate = self;
}

- (void)windowWillClose:(NSNotification *)notification
{
    [NSApp stopModal];
}

@end
当模式窗口关闭时,
NSLog(@“完成”)
运行并显示,但是在
listFunctions
完成后,我得到一个EXC\u BAD\u访问错误

启用了NSZombiesEnabled后,我得到了错误信息,即发送到解除分配实例的消息

编辑:


我使用的是ARC。

您的
函数列表窗口
列表函数:
方法中的局部变量。当该方法完成执行时,您将丢失对该对象的任何强引用,因此没有任何对象将拥有它,它将被解除分配。当模式窗口实际关闭时,它会尝试向其代理发送适当的消息,但该消息已不存在


您是否尝试在主视图控制器上将
functionListWindow
设置为实例变量?

您的
functionListWindow
listFunctions:
方法中的局部变量。当该方法完成执行时,您将丢失对该对象的任何强引用,因此没有任何对象将拥有它,它将被解除分配。当模式窗口实际关闭时,它会尝试向其代理发送适当的消息,但该消息已不存在


您是否尝试在主视图控制器上使
functionListWindow
成为实例变量?

listFunctions
方法中,首先创建
FunctionListController
对象:

- (IBAction)listFunctions:(id)sender //button clicked
{
    FunctionListController *functionListController = [[FunctionListController alloc] initWithWindowNibName:@"FunctionList"];
通过局部变量引用的;它将在范围结束时发布(方法本身)

然后获得对
functionListController窗口的引用
,并将其作为模式运行:

    NSWindow *functionListWindow = [functionListController window];

   [NSApp runModalForWindow: functionListWindow];
NSApp
将保留此对象

但是,该方法退出(
runModalForWindow
不会阻止线程),并且
functionListController
已解除分配:

   NSLog(@"done");
}
因此,您将获得一个悬空引用和一个由不再存在的对象拥有的模式窗口。因此,然后崩溃

简单地说,将
functionListController
设置为类的
strong
属性,它就会工作

新的
列表函数
如下所示:

- (IBAction)listFunctions:(id)sender //button clicked
{
    self.functionListController = [[FunctionListController alloc] initWithWindowNibName:@"FunctionList"];
    ...

listFunctions
方法中,首先创建
FunctionListController
对象:

- (IBAction)listFunctions:(id)sender //button clicked
{
    FunctionListController *functionListController = [[FunctionListController alloc] initWithWindowNibName:@"FunctionList"];
通过局部变量引用的;它将在范围结束时发布(方法本身)

然后获得对
functionListController窗口的引用
,并将其作为模式运行:

    NSWindow *functionListWindow = [functionListController window];

   [NSApp runModalForWindow: functionListWindow];
NSApp
将保留此对象

但是,该方法退出(
runModalForWindow
不会阻止线程),并且
functionListController
已解除分配:

   NSLog(@"done");
}
因此,您将获得一个悬空引用和一个由不再存在的对象拥有的模式窗口。因此,然后崩溃

简单地说,将
functionListController
设置为类的
strong
属性,它就会工作

新的
列表函数
如下所示:

- (IBAction)listFunctions:(id)sender //button clicked
{
    self.functionListController = [[FunctionListController alloc] initWithWindowNibName:@"FunctionList"];
    ...


请尝试
[functionListWindow setReleasedWhenClosed:NO]
并保持对窗口的强引用直到关闭。

请尝试
[functionListWindow setReleasedWhenClosed:NO]
并保持对窗口的强引用直到关闭。

是否使用自动引用计数?我不确定答案,但我会尝试做一些事情,因为它看起来可疑:使
functionListController
成为强属性而不是局部变量,以便它在结束时不会超出范围
listFunctions:
@PhillipMills,但是当
listFunctions:
完成时,窗口已关闭。是否使用自动引用计数?我不确定答案,但我会尝试一些方法,因为它看起来可疑:将
functionListController
设置为强属性,而不是局部变量,这样它就不会在
listFunctions:
@PhillipMills结尾处超出范围,但是当
listFunctions:
完成时,窗口已经关闭。在
listFunctions:
中关闭窗口并再次打开后,我现在获得了EXC\u BAD\u访问权限。(
[NSWindow release]:发送到解除分配实例的消息
)您能发布新的
列表函数
吗?我没有修改
列表函数
,我只将
函数列表控制器
设置为
视图控制器
的属性。我通过单击一个名为
listFunctions:
的按钮手动打开模式窗口,关闭窗口并再次单击该按钮。如果您没有修改
listFunctions
,则它仍然使用局部变量。此外,如果
runModalForWindow
未阻止线程,为什么它只在窗口关闭后输出
done
?在关闭窗口并在
listformations:
中再次打开后,我现在获得了EXC\u BAD\u访问权限。(
[NSWindow release]:发送到解除分配实例的消息
)您能发布新的
列表函数
吗?我没有修改
列表函数
,我只将
函数列表控制器
设置为
视图控制器
的属性。我通过单击一个名为
listFunctions:
的按钮手动打开模式窗口,关闭窗口并再次单击该按钮。如果您没有修改
listFunctions
,则它仍然使用局部变量。此外,如果
runModalForWindow
未阻止线程,为什么它只在获胜后输出
done