NSManagedObject and dealloc
NSManagedObject and dealloc
- Subject: NSManagedObject and dealloc
- From: "E. Wing" <email@hidden>
- Date: Tue, 27 Sep 2005 13:47:58 -0700
I have a custom subclass of NSManagedObject I am using to help me
track window positions and sizes so I can have behavior that is
similar to what Interface Builder does. So far this has proven tricky
because I need to coordinate a window controller (and window) with a
managed object. There were other complications I hit with undo and
redo of window creations and destroys that seem to be related to the
fact that the windows need to be set to return the undo manager.
Anyway, to pull this off my implementation got a little messy, and I
found it extremely convenient to give the ManagedObject an ivar
pointer to an NSWiindowController that I allocate elsewhere. I do not
need CoreData to actually save any information to disk for the window
controller, just it's position and size info and I handle the creation
of window controllers and the coordination.
By some miracle, I actually got this working. But I'm worried I might
have a memory leak. I need to release the window controller at some
point. Since I don't actually know when CoreData may release my
object, I found it convenient to tie the window controller release to
the managed object's dealloc method. But I don't understand
how/when/if dealloc is called for a ManagedObject. From the
documentation, it seems that didTurnIntoFault is the recommended
method I should use for cleanup. But this doesn't work for me.
The reason this doesn't work for me is that in order to handle window
creation/closing with undo and redo, I ended up just hiding the window
instead. (When I tried destroying and recreating a window, I got into
a "Could not merge" error sometimes.) I don't want to allocate a
brand new window controller either because then I need to start
thinking about if the window controller I need to realloc was actually
a subclass, or connected to a different nib, etc.
Hiding and revealing the window seems to work well, but I need to know
when it's safe to finally destroy the window. The problem I discovered
using didTurnIntoFault is that a managed object can turn into a fault
when it is still possible to bring back an object using undo. So if I
release my window controller in didTurnIntoFault and a fault is
triggered, my window controller is gone. Then if I undo the close the
of the window, my managed object no longer has a valid pointer to the
window controller.
So my hope/expectation for dealloc is that it is only called on a
managed object when there is no possibility that it can come back via
undo/redo. If this is true, it would seem safe to release my window
controller here. However, running tests of my program, I have never
encountered a case where dealloc seems to be called for my managed
object. So I'm wondering, if dealloc ever actually gets called by
CoreData during a run of the program or if I am just leaking memory
indefinitely.
Thanks,
Eric
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden