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: Quincey Morris <email@hidden>
- Date: Sun, 6 Jul 2008 10:40:59 -0700
On Jul 6, 2008, at 05:48, Bill Cheeseman wrote:
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
IMO it's either not safe or not necessary.
The purpose of keeping stuff out of finalize is first and foremost
about safety, and only secondarily about timing or collection-time
overhead. Collectable memory that is referred to by an object being
finalized may itself already have been finalized, causing the finalize
to crash.
If it's safe to call 'wrapup' from finalize, you may as well just move
the code into finalize and eliminate the 'wrapup' method as
bureaucratic waste.
If it's not safe to call 'wrapup' from finalize, implementing 'wrapup'
hasn't solved the problem for which it was created.
My experience has been that almost every time I've convinced myself
that it was safe to use a particular collectable object in another's
finalize, I was just plain wrong.
A safer pattern might be to replace the above finalize with:
- (void)finalize {
NSAssert (wrappedup, @"I'm sorry, Dave, I can't do that ...");
[super finalize];
}
_______________________________________________
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