• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Protecting singleton objects from releasing
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Protecting singleton objects from releasing


  • Subject: Re: Protecting singleton objects from releasing
  • From: "John C. Randolph" <email@hidden>
  • Date: Mon, 17 Feb 2003 14:09:16 -0800

On Saturday, February 15, 2003, at 02:38 PM, 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.

Can't say I agree with you here..

I doubt whether any of Cocoa's singleton objects protect programmers
from inappropriately releasing them.

Actually, most of them do, and all of them *should*. The most common technique we use in the AppKit to protect a singleton is to implement something like:

- (id) retain { return self;}
- (id) copy { return self;}
- (unsigned int) retainCount { return UINT_MAX; }
- (void) release { }

..in the singleton's class, which saves the overhead of looking up the external refcounts, etc.

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.

Singletons are a special case, and protecting them from being deallocated is simply prudent coding.

-jcr


John C. Randolph <email@hidden> (408) 974-8819
Sr. Cocoa Software Engineer,
Apple Worldwide Developer Relations
http://developer.apple.com/cocoa/index.html
_______________________________________________
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.

  • Follow-Ups:
    • Wrap up Re: Protecting singleton objects from releasing
      • From: publiclook <email@hidden>
    • Re: Protecting singleton objects from releasing
      • From: publiclook <email@hidden>
References: 
 >Re: Protecting singleton objects from releasing (From: publiclook <email@hidden>)

  • Prev by Date: Re: writeToFile:
  • Next by Date: Re: Archiving an NSMovie...
  • Previous by thread: Re: Protecting singleton objects from releasing
  • Next by thread: Re: Protecting singleton objects from releasing
  • Index(es):
    • Date
    • Thread