Disconnecting my NSDocument from its Bindings

  • I have an NSDocument based application.  I'm trying to make it robust
    in the face of the user opening and closing multiple documents.  I
    noticed that during my cocoa learning phase I had negelcted this, and
    my application's memory footprint would gradually grow and grow.
    My main thrust has been to make sure that my NSDocument has a decent
    dealloc method.  But I quickly discovered that dealloc wasn't being
    called by the standard application controls.

    I found an old message on this list suggesting that Bindings might be
    the problem.  Sure enough, my Document had a retaincount of 3 when I
    had my NSObjectController connected, but only of 1 without it.  And
    without bindings, my dealloc method is called.  That message
    suggested "breaking the bindings" during the documents close()
    method.  That sounds reasonable, but I have a few questions about it.

    1. How does one go about disconnecting from the NSObjectController?
    2. Where should this disconnect be placed?
    2. Why is this necessary?  I would think this would be somehow
    automatic.  Perhaps I've somehow broken the standard mechanism by
    some other customization.  My application was started a long time
    ago, before bindings.  Maybe I need to start over to get the most
    recent idioms.

    I've searched quit a bit on this without any direct success.  At
    least I know that others have similar problems.  But complete
    understanding eludes me.

    On the first problem (breaking bindings) I've found references to
    using NSObjectController's unbind, followed by a setContent:nil.  But
    my NSDocument doesn't have a reference to the NSObjectController.  Am
    I supposed to add an outlet for this purpose?  And where should the
    disconnect go.  In the -(void)close method??

    Any suggestions would be appreciated.

    Tom Coates :::/
  • No, what you have is known as a "retain cycle."

    Your NSDocument retains the NSObjectController since it is in the nib.
    Your NSObjectController is bound to your NSDocument and so retains it.

    Since both retain each other, neither object is ever deallocated.

    You have two options:

    1. Unbind the NSObjectController in your document's close: method
    (use the unbind: method)

    2. Create a third, model object.  Bind your NSObjectController to
    this, and also have your NSDocument retain it.  As your document is
    closed, both the NSDocument and the NSObjectController should release
    this model object and everyone should be happy.

    There may a third way of doing things that I haven't thought of of
    course!

    Mike.

    On 27 Oct 2006, at 06:20, Tom Coates wrote:

    > I have an NSDocument based application.  I'm trying to make it
    > robust in the face of the user opening and closing multiple
    > documents.  I noticed that during my cocoa learning phase I had
    > negelcted this, and my application's memory footprint would
    > gradually grow and grow.
    > My main thrust has been to make sure that my NSDocument has a
    > decent dealloc method.  But I quickly discovered that dealloc
    > wasn't being called by the standard application controls.
    >
    > I found an old message on this list suggesting that Bindings might
    > be the problem.  Sure enough, my Document had a retaincount of 3
    > when I had my NSObjectController connected, but only of 1 without
    > it.  And without bindings, my dealloc method is called.  That
    > message suggested "breaking the bindings" during the documents close
    > () method.  That sounds reasonable, but I have a few questions
    > about it.
    >
    > 1. How does one go about disconnecting from the NSObjectController?
    > 2. Where should this disconnect be placed?
    > 2. Why is this necessary?  I would think this would be somehow
    > automatic.  Perhaps I've somehow broken the standard mechanism by
    > some other customization.  My application was started a long time
    > ago, before bindings.  Maybe I need to start over to get the most
    > recent idioms.
    >
    > I've searched quit a bit on this without any direct success.  At
    > least I know that others have similar problems.  But complete
    > understanding eludes me.
    >
    > On the first problem (breaking bindings) I've found references to
    > using NSObjectController's unbind, followed by a setContent:nil.
    > But my NSDocument doesn't have a reference to the
    > NSObjectController.  Am I supposed to add an outlet for this
    > purpose?  And where should the disconnect go.  In the -(void)close
    > method??
    >
    > Any suggestions would be appreciated.
    >
    > Tom Coates :::/
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/mike.abdullah%
    > 40gmail.com
    >
    > This email sent to <mike.abdullah...>
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today