Re: Memory management of document modal panels

Graham Cox

On 26 Jul 2017, at 2:22 pm, Quincey Morris <quinceymorris@...> wrote:

If I’m reading the code correctly, the window is deterministically kept alive by the block (which captures “self” strongly). The block is kept alive by whatever object is going to invoke the completion handler (which I suspect is NSApp, because I suspect “beginSheet:" ends up at "-[NSApplication runModal:…]"). You don’t have a problem with the window controller being unretained, only with the possibility of a retain cycle, which was what the earlier reasoning was about. So you’re fine.
There’s still some “magic” about blocks I’m not altogethr clear about. When are blocks, as an actual object, created? At what point do they retain their references?

I ask because I wonder if there’s a chance that just leaving it to the block to do the retaining could lead to a window of opportunity for the object (‘self’) that it’s retaining to be autoreleased from out under it. For example, if the call to -beginSheet ends up with NSApp, will it do so after draining the current autorelease pool?

I imagine the answer’s no, and to be sure doing the extra retains myself makes sure, but I just want to know whether my previous code (with no retain/release at all) is susceptible to a race condition of some sort.

There is something of a code smell at writing “[self retain]”
Definitely, which is why I’m making sure what I’m doing is legit.


Join to automatically receive all group messages.