Re: How to completely release every object allocated when opening a NIB file ?
Re: How to completely release every object allocated when opening a NIB file ?
- Subject: Re: How to completely release every object allocated when opening a NIB file ?
- From: Jörn Salewski <email@hidden>
- Date: Mon, 12 Jan 2004 13:41:17 +0100
am 12.01.2004 10:15 Uhr schrieb Jirome Foucher unter
email@hidden:
>
Hi all,
>
>
I'm having big problems stopping my application from leaking.
>
>
- Here's the short story :
>
My app loads differents NIB files using
>
[NSBundle loadNibFile:path externalNameTable:NULL
>
withZone:NSDefaultMallocZone()];
>
>
The NIB file only holds a NSWindow which is set to release when closed.
>
The owner is a NSWindowController.
>
When the window closes, it's correctly released (I've traced it).
>
Jirome,
I've written a sort of custom WindowController, rather a NibController, that
might help.
To avoid memory leaks, all objects stored in the "topLevelNibObjects"
dictionary recieve an "extra" release message during deallocation (the owner
of the nib (=self) is responsible to do so in addition to the container
class instance they are stored in).
To make it more generic (following the approach of NSDocument) subclasses
are ment to override the - (NSString *) nibName method.
The core is as follows:
@interface TXNibController : NSObject {
@private
NSMutableArray *topLevelNibObjects;
}
- (NSString *) nibName;
@end
@implementation TXNibController
- (id) init {
if(self = [super init]) {
NSString *nibName = [self nibName];
topLevelNibObjects = [[NSMutableArray alloc] init];
id nibFile = [[NSBundle mainBundle] pathForResource:nibName
ofType:@"nib"];
id nameTable = [NSDictionary dictionaryWithObjectsAndKeys: self,
@"NSOwner", topLevelNibObjects, @"NSTopLevelObjects", nil];
[NSBundle loadNibFile:nibFile externalNameTable:nameTable
withZone:[self zone]];
}
return self;
}
- (void) dealloc {
[topLevelNibObjects makeObjectsPerformSelector:@selector(release)];
[topLevelNibObjects release];
[super dealloc];
}
- (NSString *) nibName {
return @"MyNibName";
}
@end
Hopefully this code snippet solves your problem and is usefull to you.
Yours,
Jvrn Salewski
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.