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: Bill Cheeseman <email@hidden>
- Date: Sun, 06 Jul 2008 08:48:06 -0400
- Thread-topic: Guidelines for Cocoa frameworks supporting garbage collection?
on 2008-07-05 6:14 PM, William J. Cheeseman at email@hidden wrote:
> it would be very helpful to have some good examples of ways to
> move cleanup code out of dealloc/finalize and into what you call
> "deterministic" methods.
It occurs to me that this is a question that will eventually be answered by
an evolving Cocoa design pattern, as so many other Cocoa design patterns
have evolved over time.
Apple's garbage collection documentation very strongly advises us not to
implement a -finalize method if at all possible, but instead to do cleanup
in a method whose invocation the developer can control -- in order to avoid
the problems that can be created by the built-in uncertainty as to when
-finalize will be called, not to mention the overhead that would be created
at collection time.
So here's a possible approach that was suggested to me privately, for use in
a framework that supports garbage collection. Comments?
@interface MyFrameworkClass : NSObject {
BOOL wrappedup;
}
- (void)wrapup;
- (void)someMethod;
@end
@implementation MyFrameworkClass
- (void)finalize {
if (!wrappedup) {
// protect against inadvertent early termination
[self wrapup];
}
}
- (void)wrapup {
// cleanup code goes here
wrappedup = YES;
}
- (void)someMethod {
[self wrapup];
}
@end
--
Bill Cheeseman - email@hidden
Quechee Software, Quechee, Vermont, USA
www.quecheesoftware.com
PreFab Software - www.prefabsoftware.com
_______________________________________________
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