views:

76

answers:

3

Hi,

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
}
+1  A: 

1) Could this be a simple bug in Xcode that doesn't notice the instance being stored in the dictionary?

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.

2) Anyway, why does autorelease destroy my window, if the dictionary is storing a reference to it?

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.

bbum
Sorry for the incorrection: by bug in Xcode I meant it (the static analyser) flagging my code as a possible memory leak, not the actual error and that leaves me with two things: if I ignore the warning in Xcode, my code runs. If I don't and fix it, it won't.
Rui Pacheco
Ahh... yes. It certainly could be a bug in the static analyzer. If you post your code, a better bit of analysis could be had.
bbum
A: 

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.

Peter Hosey
Toggling release when closed seemed to have no effect, the problem repeated itself exactly as before.
Rui Pacheco
not sure why this is downvoted as it was a legitimate posible solution.
darren
A: 

Make sure listOpenWindows (which really should have an Of in it) is not nil. Perhaps you forgot to create it or haven't created it yet.

Peter Hosey