Re: Releasing Menu Resources?
Re: Releasing Menu Resources?
- Subject: Re: Releasing Menu Resources?
- From: Jerry LeVan <email@hidden>
- Date: Wed, 9 Jun 2004 14:49:19 -0400
Clark,
Thanks for the heads up :) I plugged in a retain count call:
NSMenu *newMenu = [[NSMenu alloc] initWithTitle:pname];
NSLog(@" retain cnt: %d for %@", [newMenu retainCount],pname);
[menu setSubmenu:newMenu forItem:tmp];
NSLog(@" retain cnt: %d..%@", [newMenu retainCount],pname);
and get a log that looks like...
2004-06-09 14:33:06.202 SimpleEditor[1765] retain cnt: 1 for Address
Book Scripts
2004-06-09 14:33:06.203 SimpleEditor[1765] retain cnt: 2..Address Book
Scripts
2004-06-09 14:33:06.204 SimpleEditor[1765] retain cnt: 1 for iPhoto
Manager
2004-06-09 14:33:06.205 SimpleEditor[1765] retain cnt: 2..iPhoto
Manager
2004-06-09 14:33:06.206 SimpleEditor[1765] retain cnt: 1 for Portraits
& Prints
2004-06-09 14:33:06.206 SimpleEditor[1765] retain cnt: 2..Portraits &
Prints
2004-06-09 14:33:06.207 SimpleEditor[1765] retain cnt: 1 for Boost
2004-06-09 14:33:06.208 SimpleEditor[1765] retain cnt: 2..Boost
2004-06-09 14:33:06.209 SimpleEditor[1765] retain cnt: 1 for
TclTkStuff copy
2004-06-09 14:33:06.210 SimpleEditor[1765] retain cnt: 2..TclTkStuff
copy
So the setSubmenu indeed does a "retain".
Jerry
On Jun 9, 2004, at 10:06 AM, Clark Cox wrote:
On Jun 09, 2004, at 09:34, Jerry LeVan wrote:
I am adding a dynamically generated menu to one of my applications
that creates a menu of all of the sql files in a library folder
with submenus for the subdirectories. I initially create the
"Script" menu in awakeFromNib. ( The result looks like the
Script menu in the right hand side of the menu bar.)
Since users can create/add scripts on the fly I would like to
be able to update the menu on demand (or automagically).
It appears to me that I need to destroy the existing menu
structure and then call the build routine to catch any
changes.
One question is: In order to properly release the menu resources
do I need to walk the "main" menu and recursively release the
submenus and then release the items on the "main" menu?
Normally, no. Just remove the items from the menu, and that takes care
of the release (as long as no one else has retained references to the
items). However, there is one place where you should add a release to
your code to prevent a major leak, as indicated below.
For what it is worth, here is the build code that I am
using in a mini-app before placing in the main application.
It is initially called with menu set to the Script menu in
the application menubar and thePath set to the path of the
script library folder.
******************************
-(IBAction) buildScriptMenu: (NSMenu*) menu withPath:
(NSString*)thePath
{
BOOL isDir;
NSString *dirPath;
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *dirArray = [manager directoryContentsAtPath:thePath];
NSEnumerator *direnum = [dirArray objectEnumerator];
NSString *pname;
while (pname =(NSString*) [direnum nextObject]) {
// Is it a directory?
dirPath = [NSString stringWithFormat:@"%@/%@",thePath,pname];
if( ([manager fileExistsAtPath:dirPath isDirectory:&isDir] &&
isDir))
{
NSMenuItem *tmp = [menu addItemWithTitle:pname action:nil
keyEquivalent:@""];
NSMenu *newMenu = [[NSMenu alloc] initWithTitle:pname];
[menu setSubmenu:newMenu forItem:tmp];
[newMenu release];
[self buildScriptMenu: newMenu withPath:dirPath];
}
else
// if ([[pname pathExtension] isEqualToString:@"sql"])
{
if ( [pname characterAtIndex:0] == (unichar)'.') continue; //
skip dot files
NSMenuItem *tmp = [menu addItemWithTitle:pname
action:@selector(loadScript:) keyEquivalent:@""];
[tmp setRepresentedObject:dirPath];
}
}
--
Clark S. Cox III
email@hidden
http://homepage.mac.com/clarkcox3/
http://www.livejournal.com/users/clarkcox3/
_______________________________________________
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.