Re: Questions about retain count / NSOpenPanel behaviour
Re: Questions about retain count / NSOpenPanel behaviour
- Subject: Re: Questions about retain count / NSOpenPanel behaviour
- From: Shawn Erickson <email@hidden>
- Date: Mon, 11 Aug 2003 20:36:53 -0700
On Monday, August 11, 2003, at 07:56 PM, John Tsombakos wrote:
{
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
}
}
[snip]
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?!
You have sent oPanel an runModalForDirectory message. Something it does
in response to that message has sent oPanel a retain. This is not
uncommon to have happen, for example a detached thread will retain its
target, etc. It also has likely (in this case is must have otherwise
you have detected a bug) also sent oPanel an autorelease message to
remove this extra retain. It shows up with a retain count of 2 because
the current autorelease pool hasn't been deallocated, hence the needed
release(s) haven't been sent.
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?
Correct. It will get a release message when the autorelease pool it is
in is deallocated.
2. At *3, the aFile object has a retain count of 2. Again, why?!
One of those retains is from the NSArray instance; Cocoa collections
retain objects you add to them and only release them when removed or
the collection is deallocated. The other retain will likely be removed
when the current autorelease pool is deallocated.
Try playing with ObjectAlloc (/Developer/Applications/ObjectAlloc)
turning on "Also record CF & ObjC reference counting" after hitting the
go button. The instance browser my help you the most in understanding
stuff like the above.
-Shawn
_______________________________________________
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.