Last week I tried to resolve a circular refernce problem between
NSWindow and NSGraphicsContext.
The source of the problem is that when an NSWindow gets displayed it
generates an NSGraphicsContext (or rather a subclass) which will then
manage all the actual drawing. The window retain the context and the
context has an info dictionary which includes the window. That way we
already start off with a circular reference. To break this I released
the window once, to adjust the retain count. Now when the window gets
deallocated I first do a retain on it and then release the graphics
context which will in turn result in a release on the window. Or so I
hoped. There were a few issues with my original code. The graphics
context and the dictionary where autorelease objects, so I had to
postpone the deallocation of the window by returning from dealloc and
let it be called again. This also needed a bit of isa sizzling as
subclasses will not be aware of that reference counting trick and wont
be prepared to get dealloc called twice. (And the code in NSWindow
dealloc needed to be reentrant as well)
With that all resolved I expected my code now to work correctly, but
this still isn't the case. As it turns out, calling retain on an object
that already was about to get released wont have the expected result.
When a release is send to an object with a retain count of 1, that count
wont be changed! Now this means that I have to differentiate between
calls to _termianteWindow that come from dealloc, where I don't call the
retain and calls for oneShot windows, where I need to increase the
retain count before freeing the context.
All of this now looks like an even bigger mess then the one I started
with and now any help on how to clearly solve this would be welcome.
Cheers,
Fred
PS: Some of the code I talk about above is still not committed.