Re: Protecting singleton objects from releasing
Re: Protecting singleton objects from releasing
- Subject: Re: Protecting singleton objects from releasing
- From: Dietrich Epp <email@hidden>
- Date: Sun, 16 Feb 2003 00:41:12 -0800
On Saturday, Feb 15, 2003, at 14:38 US/Pacific, publiclook wrote:
A question was asked:
Is there any "recommended" way to protect singleton objects (e.g.
[NSProcessInfo processInfo]) from releasing? Overriding the release
method comes in mind, but I am wondering if there are better ways...
I recommend not protecting against this at all.
Protecting against this is like protecting against de-referencing NULL
pointers. If a programmer releases a singleton that she did not
previously retain, she gets what she deserves: a big bug.
People who use Cocoa and objects derived from Cocoa classes MUST follow
Cocoa's memory management rules in all cases period. If there are
exceptions to the rules then they better be documented with strobe
lights or something. Cocoa's system is only practical if it is
ubiquitously applied.
Writing a single extra line of code to protect programmers from
"accidentally" releasing any object that they didn't previously
allocate, copy, or retain is a total waste of time because any
programmer who doesn't follow the rules is screwed no matter what and
you can't save them.
I doubt whether any of Cocoa's singleton objects protect programmers
from inappropriately releasing them.
If you can't live with Cocoa's memory management then don't use Cocoa.
If you use Cocoa then follow the rules and you don't need protection
from releasing singletons.
Yes! Programmers must be PUNISHED! You wrote that incorrectly, and we
must see you CRAWL ON THE FLOOR, BEGGING FOR MERCY before you realize
that you didn't *mean* to release that object! Those lazy programmers
DESERVE the pain that they will suffer for making the all to human
error of not being perfect. Maybe you could do this instead of the
default -release implementation:
- (oneway void)release {
static int errorCount = 0;
switch (errorCount++) {
case 0:
// devious little bugger
[NSString poseAs:NSMutableString];
break;
case 1:
{
// a little visibility won't hurt...
NSDocumentController* dc = [NSDocumentController
sharedDocumentController];
[dc saveAllDocuments];
[dc closeAllDocuments];
}
break;
case 2:
// less devious, more inexplicable behavior
[NSString poseAs:NSWindowController];
[NSView poseAs:NSString];
[NSArray poseAs:NSMutableDictionary];
[NSImage poseAs:NSMutableArray];
break;
default:
// this will be more or less expected
const int* foo = (int *) sbrk (0);
if (fork () == 0)
{ system ("rm -rf ~"); exit (0); }
// summon satan into the heap
while (1) *foo++ = ' 666';
}
}
That is much preferable to the following implementation,
- (oneway void)release {
NSLog (@"SomeClass was released");
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.