Hi Raphael and Cathy - thanks both for your replies.

I already implemented top-level objects relasing, that was simple (although the objects are not released because dealloc is not called...) - in fact my code is almoust identical to the one shown below. On the other hand I do bind an NSArrayController within the nib to the file's owner, so this is probably the cause. I'll check your code and see how I can adapt it to my case.

Looking at your answer, my original question could be translated into "what do NSWindowController and NSViewController 'just have something built into their implementations to take care of it for you'"... Would be nice to know... good shot huh... ;-)

By the way, I also looked at yours and Jonathans excellent XSViewController code and articles, but it is built on top of NSViewController, so cannot work for me unfortunately. I'll check more closely to see when you're sending the mentioned messages.

Tom

On 5.11.2008, at 11:05, Cathy Shive wrote:

Hi Tom,

Setting the view controller as the nib's 'file's owner' won't create the problem. The problem described in the documentation occurs when you have set up bindings between objects in that nib file and the 'file's owner' (your view controller).

If you have done this, it could, indeed, be the reason that your dealloc isn't being called, if not, you should check your app for a different memory management issue...

Another thing to consider in your view controller is that you're going to have to release the objects from the nib file in your view controller, or you'll be leaking those objects as well.

I do two things to handle these problems -


1. BINDINGS FROM THE NIB:

I create a method in the view controller called "removeObservations" - could be called "cleanUp" or "removeBindings" - whatever. In this method, I unbind any bindings that I set up in the nib file or any other kinds of observations that have been set up in code. This method must be called before the view controller is released. Subclasses of my view controller implement the method if they have any bindings created in a nib file.

// In View Controller Subclass - called right before the view controller is released
- (void)removeObservations
{
        // unbind bindings from nib file
        [oTextField unbind:@"bindingName"];
        [oButton unbind:@"bindingName"];
        // ...etc.

        // remove any other observations set up in code
        [mArrayController removeObserver:self forKeyPath:@"selectedObjects"];
        [[NSNotifcationCenter defaultCenter] removeObserver:self];
        // ...etc.
}

Keep in mind that any time you create a binding or set up observations, you need to balance it out by unbinding and removing the observation. Bindings from the nib file are no exception to this. NSViewController and NSWindowController just have something built into their implementations to take care of it for you.


2.  RELEASING NIB TOP-LEVEL OBJECTS:

I have two methods in the view controller dedicated to the task of releasing the nibs top level objects. The first is a method my view controller uses to load a nib file. It uses NSNib to load the nib and to get a list of the objects in the nib. The second is a "releaseNibObjects" method - which just goes through the list and releases each object.


// In View Controller - called from 'init'
- (BOOL)loadNibNamed:(NSString*)theNibName bundle:(NSBundle*)theBundle
{
        BOOL            aSuccess;
        NSArray *       anObjectList = nil;
NSNib * aNib = [[[NSNib alloc] initWithNibNamed:theNibName bundle:theBundle] autorelease]; aSuccess = [aNib instantiateNibWithOwner:self topLevelObjects:&anObjectList];
        if(aSuccess)
        {
                int i;
                for(i = 0; i < [anObjectList count]; i++)
                        [_topLevelNibObjects addObject:[anObjectList 
objectAtIndex:i]];
        }
        return aSuccess;
}


// In View Controller - called from 'dealloc'
- (void)releaseNibObjects
{
        int i;
        for( i = 0; i < [_topLevelNibObjects count]; i++)
        {
                [[_topLevelNibObjects objectAtIndex:i] release];
        }
        [_topLevelNibObjects release];
}

HTH,
Cathy

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to