Nib & retainCount confusion

  • I have a controller class that loads a nib. The Nib has an 1)
    NSObjectController and 2) a view. The view has controls which are
    bound to various properties in the NSObjectController. The
    NSObjectcontroller's content value is bound to my controller class'
    dictionary  ivar. Whenever I send a release message to my controller
    class, I expect the dealloc method to be called. I overrode retain &
    release to see how many times the methods get called and under what
    cricumstances. It seems that whenever I call the release, the retain
    count is always at 2, so my dealloc method never gets called. This is
    a memory leak. Is my controller class being retained through bindings
    somehow? The same controls that the NSObjectController are bound to
    are also present in my controller class as IBOutlets. I set them to
    nil when I want to release the object. Ugh. Any thoughts on what I am
    doing wrong?

    LA
  • On Oct 3, 2007, at 3:05 AM, <livinginlosangeles...> wrote:

    > I have a controller class that loads a nib. The Nib has an 1)
    > NSObjectController and 2) a view. The view has controls which are
    > bound to various properties in the NSObjectController. The
    > NSObjectcontroller's content value is bound to my controller class'
    > dictionary  ivar. Whenever I send a release message to my controller
    > class, I expect the dealloc method to be called. I overrode retain &
    > release to see how many times the methods get called and under what
    > cricumstances. It seems that whenever I call the release, the retain
    > count is always at 2, so my dealloc method never gets called. This
    > is a memory leak. Is my controller class being retained through
    > bindings somehow? The same controls that the NSObjectController are
    > bound to are also present in my controller class as IBOutlets. I set
    > them to nil when I want to release the object. Ugh. Any thoughts on
    > what I am doing wrong?

    counting the retains in the first place! seriously.

      do this type of memory leak debugging using the proper tools,
    rather than trying to track the retains and releases.  search for
    "memory leaks" in Xcode to find the current tools. The Performance
    Overview document also points to the correct tools
  • Be careful that your replies don't discourage people from posting in
    the first place. I know that my code is leaking. As to whether
    counting the retains is smart, I used them to see what was invoking
    the retain. My question still stands: Is my controller class being
    retained through bindings somehow? Thanks for the referral on the
    performance overview documentation. I did use the ObjectAlloc tool
    prior to posting, examined my instance after I had released it,
    examined the backtrace again to see what was retaining it.

    LA

    On Oct 3, 2007, at 1:36 AM, Scott Anguish wrote:

    >
    > On Oct 3, 2007, at 3:05 AM, <livinginlosangeles...> wrote:
    >
    >> I have a controller class that loads a nib. The Nib has an 1)
    >> NSObjectController and 2) a view. The view has controls which are
    >> bound to various properties in the NSObjectController. The
    >> NSObjectcontroller's content value is bound to my controller
    >> class' dictionary  ivar. Whenever I send a release message to my
    >> controller class, I expect the dealloc method to be called. I
    >> overrode retain & release to see how many times the methods get
    >> called and under what cricumstances. It seems that whenever I call
    >> the release, the retain count is always at 2, so my dealloc method
    >> never gets called. This is a memory leak. Is my controller class
    >> being retained through bindings somehow? The same controls that
    >> the NSObjectController are bound to are also present in my
    >> controller class as IBOutlets. I set them to nil when I want to
    >> release the object. Ugh. Any thoughts on what I am doing wrong?
    >
    > counting the retains in the first place! seriously.
    >
    > do this type of memory leak debugging using the proper tools,
    > rather than trying to track the retains and releases.  search for
    > "memory leaks" in Xcode to find the current tools. The Performance
    > Overview document also points to the correct tools
    >
    >
  • on 10/3/07 1:05 AM, <livinginlosangeles...> purportedly said:

    > I have a controller class that loads a nib. The Nib has an 1)
    > NSObjectController and 2) a view. The view has controls which are
    > bound to various properties in the NSObjectController. The
    > NSObjectcontroller's content value is bound to my controller class'
    > dictionary  ivar. Whenever I send a release message to my controller
    > class, I expect the dealloc method to be called. I overrode retain &
    > release to see how many times the methods get called and under what
    > cricumstances. It seems that whenever I call the release, the retain
    > count is always at 2, so my dealloc method never gets called. This is
    > a memory leak. Is my controller class being retained through bindings
    > somehow? The same controls that the NSObjectController are bound to
    > are also present in my controller class as IBOutlets. I set them to
    > nil when I want to release the object. Ugh. Any thoughts on what I am
    > doing wrong?

    Your controller class is probably being implicitly retained by the binding.
    If you are "manually" loading the nib, are you releasing the top level
    objects dictionary first? Note also that if you are in some way avoiding the
    main event loop (such as using a modal session), you may have to call the
    event loop to clear the autorelease pool, with something like:
          [[NSRunLoop currentRunLoop] runUntilDate:[NSDate
    dateWithTimeIntervalSinceNow:0.1]];

    Best,

    Keary Suska
    Esoteritech, Inc.
    "Demystifying technology for your home or business"
  • Am 03.10.2007 um 09:05 schrieb <livinginlosangeles...>:
    > Is my controller class being retained through bindings somehow? The
    > same controls that the NSObjectController are bound to are also
    > present in my controller class as IBOutlets. I set them to nil when
    > I want to release the object. Ugh. Any thoughts on what I am doing
    > wrong?

      Depends on what is being bound to what. When an object observes
    another using KVO, it retains the observed object. So, if you have a
    controller that creates (and thus owns) another object, and that
    object observes the controller, then you'll probably get a retain
    circle that prevents either of them from being released, because each
    of them waits for the other to release it.

      (Or something like that, this kinda reads weird, so maybe it was
    the other way round)

      Anyway, there was a lot of hoopla around such a retain circle that
    was very easy to produce, so you may want to look up in the docs who
    is documented to retain whom and see whether this could be happening
    in your case and whether that's a problem.

      Having a retain count of 2 and an object not immediately released
    even though *you* did all your releases isn't necessarily a leak, of
    course, since the whole point of reference counting is that several
    parties can hold on to an object without either having to care about
    releasing something the other still needs. Also, if even one of them
    called autorelease, it could simply take a little while until the
    pool gets flushed (the OS occasionally doesn't delete its autorelease
    pool from an old event until it has actually received a new one, so
    your object may still be around until the next click or so).

    Cheers,
    -- M. Uli Kusterer
    http://www.zathras.de
  • I did manage to successfully release my object. I unbound the
    NSObjectController's contentObject in IB and set the
    NSObjectController's content in my controller's awakeFromNib: routine
    using [object setContent]. I found the following suggestion which
    worked: theobroma.treehouseideas.com/document.page/18

    I guess I don't understand why this should make a difference. Does
    the NSObjectController retain my controller class (which I suspect
    the binding is doing) or does it retain the object it is bound to
    that exists in my controller class?

    Keary -
    What is the "top level objects dictionary" you refer to? Is that my
    object that the controller is bound to?

    Patrick
  • on 10/3/07 3:01 PM, <livinginlosangeles...> purportedly said:

    > What is the "top level objects dictionary" you refer to? Is that my
    > object that the controller is bound to?

    When using a nib loading method such as NSBundle's
    -loadNibFile:externalNameTable:withZone:, you can maintain references to the
    top level objects (the items that appear in IB's "instance" window) in a
    dictionary. It's the only way I know of to have some control over when nib
    objects are released. If you don't do this, or you don't use an
    NSWindowController subclass (which takes care of its own nib objects), your
    nib objects will leak.

    Best,

    Keary Suska
    Esoteritech, Inc.
    "Demystifying technology for your home or business"
  • Thanks for the explanation of the top level objects.

    > When using a nib loading method such as NSBundle's
    > -loadNibFile:externalNameTable:withZone:, you can maintain
    > references to the
    > top level objects (the items that appear in IB's "instance" window)
    > in a
    > dictionary. It's the only way I know of to have some control over
    > when nib
    > objects are released. If you don't do this, or you don't use an
    > NSWindowController subclass (which takes care of its own nib
    > objects), your
    > nib objects will leak.
previous month october 2007 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