2011-03-18 57 views
0

我的應用程序在GC打開的情況下運行。Objective-c中的ShowModalWindow會導致GC上的內存泄漏?

儀器泄漏總是告訴我,這行代碼具有100%的內存泄漏:

[NSApp表示runModalForWindow:[theWindowController窗口]];

我不知道爲什麼...

這裏是整個應用程序的代碼:

/* 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 

回答

0

泄漏實際上是上面一條線一個:

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

你」重新分配一個modalWindowController並將其分配給一個本地指針。當方法結束時,指針將超出範圍,但不會釋放您分配的對象。此時,你不再有辦法引用該對象(不再有指針),所以你將來無法釋放它。這是一個泄漏。

+0

感謝您的回覆。應用程序項目已開啓Garbage Collection,因此WindowController將由垃圾收集收集?這就是我從GC手冊中讀到的內容,當GC開啓時,autorelease將無用。 – 2011-03-18 17:19:17