On Mar 16, 2009, at 2:32 PM, Michael Tsai wrote:

On Mar 16, 2009, at 2:08 PM, Bill Cheeseman wrote:

I am looking for a strategy to avoid implementing a -finalize method in a garbage-collected Objective-C class that declares a CFTypeRef-style Core Foundation instance variable. I believe this is a fairly common problem, because any Cocoa wrapper class for a Core Foundation API would have to do it, but I haven't seen a usable solution. Apple's GC documentation stops just short of suggesting an answer.


I believe you're supposed to declare your CFTypeRef ivar as __strong and call CFMakeCollectable so that the garbage collector will be responsible for releasing it. You can still use CFRelease in - dealloc for when you aren't running under garbage collection.

After thinking on this overnight, I have concluded that in a mixed- mode framework the best practice is indeed to call CFMakeCollectable as soon as the CFTypeRef ivar is created so that garbage-collected clients can ignore memory management issues.

As a corollary, when the problem domain served by the framework is such that there is no convenient way to know when to call an - invalidate method, simply put all the cleanup code in -finalize and be done with it.

But perhaps even in this situation, it would be good practice to provide -invalidate and -isValid methods, and write -finalize so that it checks -isValid before calling -invalidate itself. This way, clients who do know when to call -invalidate can do so.

This leaves unresolved the need in some cases to do cleanup work before -finalize because of order-of-execution issues with -finalize. My tentative conclusion is that this is a huge can of worms for garbage collection when CFTypeRef ivars are involved.

--

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