在Objective-C ShowModalWindow会导致与GC内存泄漏呢?

问题描述 投票:0回答:1

我的应用程序正在运行与GC打开。

仪器泄漏总是告诉我,这行代码有100%的内存泄漏:

[NSApp runModalForWindow:[theWindowController window]];

我不知道为什么。

这里是整个应用程序的代码:

/* delegate */

#import "m_ModalWindowAppDelegate.h"
#import "modalWindowController.h"

@implementation m_ModalWindowAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application 
}

- (IBAction) openModalWindowButtonClicked: (id) sender
{
    modalWindowController *theWindowController = [[modalWindowController alloc] init];

    [NSApp runModalForWindow:[theWindowController window]];
    [NSApp endSheet: [theWindowController window]];
    [[theWindowController window] orderOut:self];
}

@end


/* modalWindowController */

#import "modalWindowController.h"


@implementation modalWindowController

- (id) init
{
    self = [self initWithWindowNibName:@"modalWindow"];

    return self;
}


- (IBAction) closeButtonClicked:(id)sender
{
    [NSApp stopModal];
}

@end
objective-c memory-leaks garbage-collection
1个回答
0
投票

泄漏实际上是一条线的一个以上:

modalWindowController *theWindowController = [[modalWindowController alloc] init];

你分配modalWindowController并将其分配给当地的指针。当该方法结束时,指针超出范围,但你永远不释放你分配的对象。在这一点上,你不再有办法来指代对象(没有更多的指针),所以你不能在未来释放。这是一个泄漏。

© www.soinside.com 2019 - 2024. All rights reserved.