of nibs and top level objects
of nibs and top level objects
- Subject: of nibs and top level objects
- From: Paul Forgey <email@hidden>
- Date: Mon, 10 Apr 2006 00:06:36 -0700
Searching the archives, I've seen questions like this asked on this
list without any real answers.
My window controller may, at a certain time as a consequence of a
deep child item somewhere in the tree controller's hierarchy, load a
supplemental nib file owned by the node. When the node is no longer
needed, it is told to "unload" and hopefully releases all the top
level nib items.
I'm having a lot of trouble doing this. I've managed to account for
about 14 retain counts, which is about 14 short of the total needed
to bring the total count back to 0. I know for a fact I am leaking
because I put in some code to manually log the init's and dealloc's
of all my nodes. If the node loads a nib file, it never goes away.
In the list archives, I stumbled over this bit of code following and
it seems to be giving back a reasonable list of items. By retaining
the array I hold onto the items, then release the array when I'm done.
What more do I need to do, and is there clearer documentation than
what's in the Application Kit section about NSBundle additions which
just says I need to "release all the top level items" with no further
information about how to actually _do_ this?
One thing that may be complicating matters is there are bindings in
this nib referring to objects in the document window controller's
nib. That object owning the document nib is not over-retained. It
still dealloc's when the document closes.
This code was found in a posting from James DiPalma back from
September 26, 2003. I can't remember if that predates Cocoa Bindings
or not..
+ (NSArray*) topLevelObjectsFromLoadingNibNamed:(NSString*)name owner:
(id)owner
{
NSBundle * bundle;
NSString * file;
file = nil;
bundle = [NSBundle bundleForClass:[owner class]];
if (nil != bundle) {
file = [bundle pathForResource:name ofType:@"nib"];
}
// Documentation for loadNibNamed states that NSBundle only
searches for nib name in mainBundle if there
// is no bundle for owner's class. Search in mainBundle anyway?
if (nil == file) {
bundle = [NSBundle mainBundle];
file = [bundle pathForResource:name ofType:@"nib"];
}
// WARNING what does loadNibNamed: do if a nib file does not exist?
return [NSBundle topLevelObjectsFromLoadingNibFile:file
owner:owner];
}
+ (NSArray*) topLevelObjectsFromLoadingNibFile:(NSString*)file owner:
(id)owner
{
NSDictionary * nameTable;
NSMutableArray * topLevelObjects;
topLevelObjects = [NSMutableArray array];
nameTable = [NSDictionary
dictionaryWithObjectsAndKeys:topLevelObjects, @"NSTopLevelObjects",
owner,
@"NSOwner", nil];
if ([NSBundle loadNibFile:file externalNameTable:nameTable
withZone:[owner zone]]) {
// nibInstantiateWithOwner retains each top level object
[topLevelObjects makeObjectsPerformSelector:@selector
(release)];
return topLevelObjects;
}
return nil;
}
_______________________________________________
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