Garbage collection, leaks and drains

  • Hi list,

    I'm having a little bit of a brain fuse while building my first
    garbage-collected app as I a unsure if I'm running the risk of leaks
    or messaging deallocated objects. It's a fairly standard NSDocument
    based application, with a source-list and main view in the document
    window. The main view is an NSBox, which has a view substituted in
    when the selection in the source list changes.

    Most of these views are in separate nib files and their file's owner
    is a similarly named class (for example GroupView.nib has
    GroupViewManager.h/m). The portion of code I am using for this swap is
    as follows (psuedo-code in places):

    if ( newSelection is of type 'Group' )
    {
      GroupViewManager *groupViewManager = [[GroupViewManager alloc] init];
      [NSBundle loadNibNamed:@"GroupView" owner:groupViewManager];
      [contentBox setContentView:[groupViewManager mainView]];
    }
    else
    {
      [contentBox setContentView:fallbackView];
    }

    where contentBox is the NSBox and fallbackView a default NSView.

    I can't see any obvious leaks in ZoneMonitor, nor are there any
    crashes from messages deallocated objects, but this code just doesn't
    seem *right*. Is this the sensible way of doing things? Or am I just
    being overly neurotic?

    Any replies that can clear this up will be much appreciated.

    -- Ben
  • On Jan 3, 2008, at 11:03 AM, Ben wrote:
    > I can't see any obvious leaks in ZoneMonitor, nor are there any
    > crashes from messages deallocated objects, but this code just
    > doesn't seem *right*. Is this the sensible way of doing things? Or
    > am I just being overly neurotic?

    You are being overly neurotic.  Relax, and let the garbage collector
    clean up for you. ;)

    Seriously, though, the code looks just fine.

    b.bum
  • On Jan 3, 2008, at 11:03 AM, Ben wrote:

    > Most of these views are in separate nib files and their file's owner
    > is a similarly named class (for example GroupView.nib has
    > GroupViewManager.h/m).

    Bill already addressed your GC concerns, but I thought this would be a
    good example of the use of another new Leopard Cocoa feature,
    NSViewController.  The point of an NSViewController is to act as the
    owner of an "independent" view hierarchy, just like you're doing with
    your ViewManager classes.

    NSViewController Class Reference
    <http://developer.apple.com/documentation/Cocoa/Reference/NSViewController_C
    lass/Introduction/Introduction.html
    >

    What I do is create an application-specific base class that derives
    from NSViewController (call it MyAppViewController) that implements an
    -init method like this:

    - (id)init {
        return [super initWithNibName:[self nibNameForClassName]
                                bundle:[NSBundle bundleForClass:[self
    class]]];
    }

    I also implement a -nibNameForClassName method that gets the name of
    the current class and lops off "Controller" (and possibly my custom
    prefix, too) so that I don't litter my code with nib names; instead,
    they're inherent in the class name.

    One other thing I find it useful to do when subclassing
    NSViewController is override -loadView to implement pre/post-load
    hooks like NSWindowController has:

    - (void)viewWillLoad {
        // Here for subclasses to override.
    }

    - (void)viewDidLoad {
        // Here for subclasses to override.
    }

    - (void)loadView {
        [self viewWillLoad];
        [super loadView];
        [self viewDidLoad];
    }

    These hooks have come in handy occasionally.  There's also -
    awakeFromNib, of course, but I generally try to only implement things-
    I-wish-I-could-do-in-IB in that method (e.g. setting the double action
    of a table view), and perform more controller-level operations (such
    as telling a data source to fetch model objects) in a -
    {window,view}DidLoad method.

      -- Chris
previous month january 2008 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