2016-01-21 40 views
1

我我的程序的執行過程中收到一個警告在Xcode:當應用程序提供了一個NSOpenPanel選擇將異步加載某些文件時出現NSOpenPanel和檢測到泄漏觀察員

2016-01-21 03:19:26.468 IsoMetadonnees[1975:303] An instance 0x1004eefd0 of class NSVBOpenPanel was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info: 
    <NSKeyValueObservationInfo 0x608000444710> (
    <NSKeyValueObservance 0x6080000d5310: Observer: 0x100592cf0, Key path: level, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x6080004486a0> 
) 

的問題。該應用程序不會崩潰和文件正確加載...

我不創建任何值觀察員,所以我想象觀察者是由NSOpenPanel創建的,但我不知道任何過程,以移除觀察者,我還沒有創建...

儘管有這個警告,我做了多個負載,沒有通知任何崩潰。我使用我的應用程序多年以來沒有任何問題,但我最近改用ARC;可能是此時出現(或被檢測到)的問題。

這裏是我的代碼的簡化版本:前

- (IBAction)ajoutFichier:(id)sender { 
    NSOpenPanel *openPanel = [NSOpenPanel openPanel]; 
    // Here some configurations of openPanel 

    if ([openPanel runModal] == NSOKButton) { 
     tmp_listeURLFichiers = [openPanel URLs]; 
    } 
    //[openPanel close]; // I add this code unsuccessfully 
    openPanel = nil; // I add this code unsuccessfully 

    // I call a task in back ground to load my files 
    if ((tmp_listeURLFichiers != nil) && ([tmp_listeURLFichiers count]>0)) 
     [self performSelectorInBackground:@selector(ajouteListeFichiers:) withObject:tmp_listeURLFichiers]; 
} 

// Load files in background 
-(BOOL) ajouteListeFichiers:(NSArray *)listeDesFichierAAjouter { 
    @autoreleasepool { 
     // Some stuff to show a progress bar 

     // Loop to load selected files 
     for (id tmpCheminVersMonImage in listeDesFichierAAjouter) { 
      // Load files 
     } 


    } // <========== THE WARNING OCCURS AT THIS POINT, WHEN autoreleasepool is cleaned 
    return (YES); 
}  

我嘗試添加

[openPanel close]; 

openPanel = nil; 

強制從內存中釋放openPanel(因而觀察者)開始後臺任務,但這並沒有改變任何東西...

你有什麼想法嗎?

謝謝你的幫助!

+0

什麼是'NSVBOpenPanel'?你是否添加了觀察者?你有沒有在NSKVODeallocateBreak上設置一個斷點,然後給觀察者設置斷點? – Willeke

+0

我不知道NSVBOpenPanel是什麼...我只使用NSOpenPanel。我想我們使用沙箱時,NSVBOpenPanel是一種NSOpenPanel。 我按照警告告訴我要做的方式設置了一個斷點,並且我可以確定警告發生在我的第一篇文章中的autorelease池(我在代碼中指出的點)處。 我沒有添加任何觀察者... – brolive

回答

0

我可以使用下面的技巧解決問題:

我聲明一個變量在我的視圖控制器:

__strong NSOpenPanel *prgOpenPanel; 

然後我用它在我的代碼

//NSOpenPanel *prgOpenPanel = [NSOpenPanel openPanel]; 
self.prgOpenPanel = nil; 
self.prgOpenPanel = [NSOpenPanel openPanel]; 
// Here some configurations of openPanel 

if ([prgOpenPanel runModal] == NSOKButton) { 
    tmp_listeURLFichiers = [prgOpenPanel URLs]; 
    if ((tmp_listeURLFichiers != nil) && ([tmp_listeURLFichiers count]>0)) 
     [self performSelectorInBackground:@selector(ajouteListeFichiers:) withObject:tmp_listeURLFichiers]; 
} 

沒有更多警告!