2013-01-12 53 views
0

這裏是我的代碼模板:爲什麼執行「[pool drain]」後,它永遠不會返回給調用者?

int main(int argc, char *argv[]) { 
    // create an autorelease pool 
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
    // make sure the application singleton has been instantiated 
    NSApplication * application = [NSApplication sharedApplication]; 
    // instantiate our application delegate 
    AppDelegate * applicationDelegate = 
          [[[AppDelegate alloc] init] autorelease]; 
    // assign our delegate to the NSApplication 
    [application setDelegate:applicationDelegate]; 
    // call the run method of our application 
    [application run]; 
    // drain the autorelease pool 
    [pool drain]; 
    // execution never gets here .. 
    return 0; 
} 

「[池排水]」 通過 「返回0」 followd爲什麼從來沒有得到執行。

但是,我發現了另一個gnustep官方的例子,它做了同樣的事情。

int 
main(int argc, char **argv, char** env) 
{ 
    id pool = [NSAutoreleasePool new]; 
    NSApplication *theApp; 

#if LIB_FOUNDATION_LIBRARY 
    [NSProcessInfo initializeWithArguments:argv count:argc environment:env]; 
#endif 

    theApp = [NSApplication sharedApplication]; 
    [theApp setDelegate: [browserController new]]; 
    [theApp run]; 

    [pool release]; 

    return 0; 
} 

來證明這一點「[該app運行]」從來沒有回來,我已經做了與添加一個無限循環的做法「[該app運行]。」但它從來沒有得到執行之後。爲什麼GNUSTEP官員的例子會這樣做?

回答

4

你確定[pool drain]實際上被調用嗎? [application run]將不會返回,除非調用[NSApp stop](這很罕見)。如果比較常見的[NSApp terminate]被調用,爲the docs say

不要刻意把最後的清理代碼在你的應用程序的main()函數 ,它永遠不會被執行。如果需要清理,則執行 ,清理代理的applicationWillTerminate:方法。

一旦您將應用程序交給run,您通常不會讓它恢復原狀。

+0

嗨@羅布納皮爾,我編輯了以前的帖子。 –

+0

我檢查了stackoverlow上的過去帖子,與我的問題有關。它說很久以前有bug存在。 –

+0

我不確定你在這裏的意思。沒有錯誤。它的行爲如文件記錄。 - 運行不保證返回。 –

相關問題