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: "Clark Cox" <email@hidden>
- Date: Sun, 6 Jul 2008 07:50:16 -0700
On Sun, Jul 6, 2008 at 5:45 AM, Bill Cheeseman <email@hidden> wrote:
> on 2008-07-06 2:45 AM, Quincey Morris at email@hidden wrote:
>
>> It also occurs to me that the OP is going to have to be careful to
>> document the behavior of methods in *his* framework that return new CF
>> objects (if there are any). He'll have to decide whether to use CF
>> rules (retain count == 1), Cocoa rules (retain count == 1 or
>> autoreleased object, depending on whether the caller or the framework
>> is regarded as the owner of the new object), or to return an object on
>> which CFMakeCollectable has already been called.
>
> Actually, I (the OP) am receiving inconsistent advice privately regarding
> the original question (the OQ?), namely, whether to use the __strong keyword
> when declaring an instance variable with a CFType-derived type. Privately, I
> have been told by a knowledgeable developer that I do NOT need to use
> __strong, if I have been careful in my framework to balance my calls to
> CFRetain and CFRelease.
If CFRetain/CFRelease are indeed balanced, then you don't need the
__strong keyword *but* this requires that you implement -finalize to
CFRelease your ivars. You should, instead, use __strong, and
CFMakeCollectable, and eliminate the need to implement -finalize.
For example. In option 1 below, the object pointed to by "ivar" won't
likely be collected until the cycle *after* the MyObject instance is
collected, while in option 2, it is collected as part of the same
cycle. By using option 1, you are causing extra work for the collector
and potentially raising your memory high-water mark.
//Option1:
@interface MyObject : NSObject {
CFTypeRef ivar;
}
-(void)createIvar;
@end
@implementation MyObject
-(void)dealloc {
if(ivar) CFRelease(ivar);
[super dealloc];
}
-(void)finalize {
if(ivar) CFRelease(ivar);
[super finalize];
}
-(void)createIvar {
ivar = CFFooCreateFoo(...);
}
@end
//Option2:
@interface MyObject : NSObject {
__strong CFTypeRef ivar;
}
-(void)createIvar;
@end
@implementation MyObject
-(void)dealloc {
if(ivar) CFRelease(ivar);
[super dealloc];
}
-(void)createIvar {
ivar = CFMakeCollectable(CFFooCreateFoo(...));
}
@end
> He even goes so far as to suggest that it would be
> dangerous or at least inefficient to use __strong in a mixed environment
> such as a shared framework that supports both garbage collection and
> retain/release, due in part to the additional overhead.
What "additional overhead" does he imagine will be incurred?
--
Clark S. Cox III
email@hidden
_______________________________________________
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