使用字典进行内存管理/GCC 可能会误解?
在我的代码中的某个地方,我需要从主窗口创建多个窗口,每个窗口都具有特定的配置,但所有实例都是同一控制器对象。
我还需要保留打开的窗口列表,因此每当我打开一个窗口时,我都会将其实例存储在字典中,当窗口关闭时,我会向主窗口发送一条通知,该通知会触发一个方法,然后从该窗口中删除该特定窗口。字典。
我通过创建控制器对象的实例然后在其上调用 [showWindow:self] 来创建窗口。然后我将窗口存储在字典中并退出该方法。
我的问题是,我既不释放也不自动释放新创建的对象,因为从字典中删除窗口时应该这样做(对吗?)。如果我释放或自动释放该对象,则在将其存储在字典中后,当我尝试从字典中删除该对象时,我会收到各种错误。
1)这可能是 Xcode 中的一个简单错误,没有注意到实例存储在字典中吗?
2)无论如何,如果字典存储了对它的引用,为什么 autorelease 会破坏我的窗口?
[更新] 代码如下
CHPostgreSQLMainController *pgMainController = [[CHPostgreSQLMainController alloc]initWithConnectionSettings:(CHPostgreSQLSettingsModel *)entityFromArray error:&error];
// Only display the window if the connection was successful.
if (pgMainController) {
[pgMainController showWindow:self];
// Register the window we've opened on the list of open windows
[listOpenWindows setObject:pgMainController forKey:[entityFromArray connectionName]];
} else {
//call NSAlert
}
Somewhere on my code I need to create several windows from a main window, each functioning with a specific configuration but all instances of the same controller object.
I also need to keep a list of open windows, so whenever I open a window I store its instance in a dictionary and when the window is closed I send a notification to the main window which fires a method that then removes that specific window from the dictionary.
I create windows by creating an instance of their controller object and then calling [showWindow:self] on it. I then store the window in the dictionary and exit the method.
My problem is that I'm neither releasing nor autoreleasing the newly created object as that should be done when the window is removed from the dictionary (right?). If I do release or autorelease that object, after I store it in the dictionary, I will get all sorts of errors when I try to remove the object from the dictionary.
1) Could this be a simple bug in Xcode that doesn't notice the instance being stored in the dictionary?
2) Anyway, why does autorelease destroy my window, if the dictionary is storing a reference to it?
[Update]
Code below
CHPostgreSQLMainController *pgMainController = [[CHPostgreSQLMainController alloc]initWithConnectionSettings:(CHPostgreSQLSettingsModel *)entityFromArray error:&error];
// Only display the window if the connection was successful.
if (pgMainController) {
[pgMainController showWindow:self];
// Register the window we've opened on the list of open windows
[listOpenWindows setObject:pgMainController forKey:[entityFromArray connectionName]];
} else {
//call NSAlert
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不会。保留、释放和自动释放的发生是因为您编写了使它们发生的代码。 Xcode 不会在您的代码中插入任何此类行为。
现在,它肯定可能是静态分析器中的错误(正如您所指出的)。请发布更多代码。
您是否将窗口保留在某处,以便需要平衡保留与释放或自动释放?
如果没有,则说明您过度释放了窗口。
尝试运行静态分析器(构建和分析)并修复它发现的任何问题。
无论如何,如果您没有
+alloc
窗口并且没有-retain
窗口,则不应释放它。No. Retains, releases and autoreleases happen because you wrote the code to make them happen. Xcode doesn't insert any such behavior into your code.
Now, it certainly could be a bug in the static analyzer (as you indicate). Please post more code.
Did you retain the window somewhere such that you need to balance the retain with a release or autorelease?
If not, you are over-releasing the window.
Try running the static analyzer (build & analyze) and fix any problems it identifies.
In any case, if you didn't
+alloc
the window and you didn't-retain
the window, you shouldn't be releasing it.确保窗口的“关闭时释放”设置已关闭(或者您正在对其进行补偿)。如果它打开,当用户关闭它时它会自行释放。
Make sure that the window's “release when closed” setting is turned off (or that you're compensating for it). If it's on, it released itself when the user closed it.
确保
listOpenWindows
(实际上应该有一个Of
)不是nil
。也许您忘记创建它或尚未创建它。Make sure
listOpenWindows
(which really should have anOf
in it) is notnil
. Perhaps you forgot to create it or haven't created it yet.