Questions about retain count / NSOpenPanel behaviour
Questions about retain count / NSOpenPanel behaviour
- Subject: Questions about retain count / NSOpenPanel behaviour
- From: John Tsombakos <email@hidden>
- Date: Mon, 11 Aug 2003 22:56:06 -0400
I thought I was finally getting the whole retail/release thing (if you
create it, you must release it / convience constructors return an
autoreleased object, etc).
I have a little routine that reads in an XML file (a .loc file
downloaded from geocaching.com), and fill in some TextFields in a
window. Here is the routine, with some lines marked. Questions are
below. (please disregard some logic errors...)
{
NSURL *theURL;
NSString *aFile;
int result;
NSArray *fileTypes = [NSArray arrayWithObject:@"loc"];
NSOpenPanel *oPanel = [NSOpenPanel openPanel];
// * 1
result = [oPanel runModalForDirectory:nil file:nil types:fileTypes];
// * 2
if (result == NSOKButton)
{
NSArray *filesToOpen = [oPanel filenames];
int i, count = [filesToOpen count];
for (i=0; i<count; i++) {
aFile = [filesToOpen objectAtIndex:i];
// * 3
}
}
theURL = [NSURL fileURLWithPath: aFile];
if (!theURL)
NSLog(@"theURL is null");
XMLTree *tree = [[XMLTree alloc] initWithURL:theURL];
[tree retain];
if (!tree)
NSLog(@"tree is null");
[locName setStringValue:[[tree descendentNamed:@"name"] xml]];
[locLatitude setStringValue:[[tree descendentNamed:@"coord"]
attributeNamed:@"lat"]];
[locLongitude setStringValue:[[tree descendentNamed:@"coord"]
attributeNamed:@"lon"]];
NSString *det = [[tree descendentNamed:@"link"] description];
det = [det stringByTrimmingCharactersInSet: [NSCharacterSet
whitespaceAndNewlineCharacterSet]];
[locDetails setStringValue:det];
}
The questions:
1. At * 1, the retain count of oPanel is 1 (as shown using the debugger
- print (int) [oPanel retainCount]). When I step over the next line,
runModalForDirectory, at *2, the retain count of oPanel is now 2. Huh?!
Why would it change?! I also know that since I used oPanel =
[NSOpenPanel openPanel] I'm given an autoreleased object and I
shouldn't have to release it, correct?
1a. If I use, for the forDirectory parameter, NSHomeDirectory(), it
does not go to my home directory. The file browser comes up and just
shows the top level of my system. Even choosing Home or any shortcut in
the dropdown menu does not cause it to change. I have to click on the
drive and navigate all the way down. Why? using nil will make it start
in the apps directory just fine.
2. At *3, the aFile object has a retain count of 2. Again, why?!
thanks in advance,
John T.
_______________________________________________
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.