Re: Guidelines for Cocoa frameworks supporting garbage collection?
Re: Guidelines for Cocoa frameworks supporting garbage collection?
- Subject: Re: Guidelines for Cocoa frameworks supporting garbage collection?
- From: Chris Hanson <email@hidden>
- Date: Sat, 05 Jul 2008 14:56:46 -0700
On Jul 5, 2008, at 2:23 PM, Bill Cheeseman wrote:
So, I guess my frameworks should declare ...
__strong CGEventRef myEvent;
__strong AXUIElementRef myUIElement;
... if I am going to advertise the frameworks as supporting both
retain/release and garbage collection. Is that right?
That's correct.
Note that object lifetimes of even CF objects will be different under
GC: CF objects will behave like Objective-C objects in that they will
be collected when they are no longer referenced, rather than when
their reference count goes to 0.
Under GC, CFRetain/CFRelease still manipulate an object's reference
count, and are therefore *not* isomorphic to -retain/-release as they
are under non-GC. So you need to be careful with how you use toll-
free bridged objects. For example, this pseudocode is wrong under GC:
- (NSURL *)someURL {
NSURL *URL = (NSURL *)CFURLCreate...(...);
return [URL autorelease];
}
It will leak, because the runtime will "eat" the -autorelease message
when running under GC. The correct way to write this is:
- (NSURL *)someURL {
NSURL *URL = NSMakeCollectable(CFURLCreate...(...));
return [URL autorelease];
}
When running under GC, the call to NSMakeCollectable will balance the
initial retain count of of 1 for the CF object. This ensures that the
CF object will be collected when there are no more references to it.
When running under non-GC, the call to NSMakeCollectable does nothing,
so the -autorelease balances the initial retain count of 1.
I've already included
-finalize methods to complement the -dealloc methods. I don't think
I need
to do anything else, but I'm far from sure.
I haven't looked at your framework, but I do want to point out that -
finalize methods should be relatively rare; generally speaking, you
shouldn't need a -finalize everywhere you need a -dealloc.
Often people will write a -finalize method because they need to ensure
some scarce resource (like a file handle) is released when their
object is no longer referenced. It's good to have this as a safety
net, but it's also good to have some form of -close or -invalidate
method that will clear out scarce resources at a deterministic point.
NSFileHandle is a good example of this even outside of GC: An
NSFileHandle *can* close its file descriptor on -dealloc, but
generally you'll want to send an instance -closeFile as soon as you're
done with it so you don't risk running out of file descriptors if the
lifetime of the NSFileHandle instance has been extended.
-- Chris
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden