我的应用程序正在运行与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
泄漏实际上是一条线的一个以上:
modalWindowController *theWindowController = [[modalWindowController alloc] init];
你分配modalWindowController并将其分配给当地的指针。当该方法结束时,指针超出范围,但你永远不释放你分配的对象。在这一点上,你不再有办法来指代对象(没有更多的指针),所以你不能在未来释放。这是一个泄漏。