• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to completely release every object allocated when opening a NIB file ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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: Charles Srstka <email@hidden>
  • Date: Tue, 13 Jan 2004 16:07:06 -0600

Thanks for the code snippet.

However, I can't find mention of @"NSTopLevelObjects" in any of the headers or documentation. Is this an undocumented technique, and thus is it possible that it could stop working in future versions of Mac OS X?

How far back does this trick work? Does it work in 10.1?

Charles

On Jan 12, 2004, at 6:41 AM, Jvrn Salewski wrote:

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.
_______________________________________________
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.

  • Follow-Ups:
    • Re: How to completely release every object allocated when opening a NIB file ?
      • From: j o a r <email@hidden>
  • Prev by Date: Re: Setting a documents file type during save as=8 5
  • Next by Date: Re: Setting a documents file type during save as=8 5
  • Previous by thread: Re: Setting a documents file type during save as=8 5
  • Next by thread: Re: How to completely release every object allocated when opening a NIB file ?
  • Index(es):
    • Date
    • Thread