Re: Questions about retain count / NSOpenPanel behaviour
Re: Questions about retain count / NSOpenPanel behaviour
- Subject: Re: Questions about retain count / NSOpenPanel behaviour
- From: John Tsombakos <email@hidden>
- Date: Mon, 11 Aug 2003 23:50:46 -0400
On Monday, August 11, 2003, at 11:36 PM, Shawn Erickson wrote:
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.
Ok. That makes sense. Basically I don't need to do anything else.
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.
Just so I know = aFile is just pointing to some (NSString) object that
was created in the openpanel routine. It has a ref count of 2 because
it was most likely retained once when created, then again when it was
added to the collection (array). So once again I shouldn't have to do
anything - I'm not responsible for releasing anything here? ie. don't
worry about it :)
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.
I did try that but the routine is in the action method of a button and
it flys by before I could tell anything happened. I'll play around
again.
Thanks for the help, I'm that much closer to Cocoa enlightenment.
john
_______________________________________________
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.