On Mar 16, 2009, at 3:48 PM, Quincey Morris wrote:

Assuming you do *not* call CFMakeCollectable or NSMakeCollectable, it's perfectly OK to have a finalize method that calls CFRelease. Because the CF instance variable lifetime is *not* GC-managed in that case, there's no chance that GC will have collected the CF object in advance. The dire warnings about finalize are based on the indeterminate order of collection of *collectable* objects.

Michael Tsai's suggestion of making the CF object collectable, so that it's collected automatically, and doing without the finalize method, is another perfectly good approach. However, there are some non-CF objects that can't be made collectable like this (e.g. CV...Ref). For those, you would use the CFRelease-in-finalize technique.

It strikes me that this doesn't answer my question, either.

My question is how to know when to call my -invalidate method to clean up everything related to the ivar once I am through with the ivar. Doing it in a -finalize method should work, but it is strongly discouraged by Apple -- not only because of the indeterminate order of collection but more because it is inappropriate to load up GC collection with a lot of time-consuming activity.

As noted in my reply to Michael Tsai, I do not call CFMakeCollectable. But I DO declare my CFTypeRef variable as __strong to make sure it is known to the garbage collector. If I have read the Guide correctly, it is not CFMakeCollectable that makes the ivar known to the garbage collector; that's what the __strong declaration is for. CFMakeCollectable merely makes it immediately eligible for collection by decrementing its retain count to 0. Thus, it seems to me that it is incorrect to say that the ivar lifetime is not managed by GC just because I did not call CFMakeCollectable. In fact, it will be managed by GC, but only after I call my -invalidate method and thereby decrement the ivar's retain count to 0.

Whether I do it your way or Michael's way, I still face the question of knowing when to call the -invalidate method that is recommended by Apple. In -invalidate, I will do a number of things in addition to decrementing the CFRetain count of the ivar, and I must do them at the right time -- neither too soon nor too late. I will do them in - finalize if I must, but Apple won't like that.

--

Bill Cheeseman
b...@cheeseman.name

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to