gpt4 book ai didi

objective-c - NSOpenPanel 和检测到泄漏的观察者

转载 作者:行者123 更新时间:2023-12-03 17:19:50 24 4
gpt4 key购买 nike

我在执行程序期间收到了来自 XCode 的警告:

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 来选择一些将异步加载的文件时,就会出现问题。应用程序不会崩溃并且文件已正确加载...

我没有创建任何值观察者,所以我想象观察者是由 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,但这不会改变任何内容...

你有什么想法吗?

感谢您的帮助!

最佳答案

我可以使用以下技巧解决该问题:

我在 View Controller 中声明一个变量:

__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];
}

不再有警告!

关于objective-c - NSOpenPanel 和检测到泄漏的观察者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34918338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com