• 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: 'Pseudo' Singleton in Objective C
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 'Pseudo' Singleton in Objective C


  • Subject: Re: 'Pseudo' Singleton in Objective C
  • From: Jens Alfke <email@hidden>
  • Date: Thu, 14 Mar 2013 11:38:26 -0700

On Mar 14, 2013, at 11:22 AM, Pax <email@hidden> wrote:

> My issue is that I only want one instance of the Information class to be loaded at a given time (and therefore only one information window on screen at a given time).  If the information window is already loaded and it is then reselected from the menu then the existing window needs to be brought to the front - nothing more.
> I have currently resolved it by making my class a singleton - but that's not the right answer because it means that I can't release information window and its class when it gets closed, resulting in a minor leak (only minor because, as a singleton, the instance is only ever loaded once anyway).

IMHO the full belt-and-suspenders implementation of a singleton you’ve chosen — overriding alloc, retain, release, etc. — is overkill. I just implement singletons using a +sharedInstance method that looks like your +singleton. If I’m feeling paranoid I put an assertion into the -init method that the singleton doesn’t yet exist.

An easy way to make a sort of “weak singleton” like what you want, is to (a) make the static singleton pointer variable non-retained, and (b) nil it out in the -dealloc method. That way your singleton object isn’t kept around permanently, and when it goes away it cleans up so that the next request will instantiate a new one. (Of course you do have to ensure that *something* retains your singleton object as long as it needs to stay around, or else it’ll get dealloced very soon after being created and lose its state.

—Jens
_______________________________________________

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


References: 
 >'Pseudo' Singleton in Objective C (From: Pax <email@hidden>)

  • Prev by Date: Re: Snapping windows while moving
  • Next by Date: Re: 'Pseudo' Singleton in Objective C
  • Previous by thread: 'Pseudo' Singleton in Objective C
  • Next by thread: Re: 'Pseudo' Singleton in Objective C
  • Index(es):
    • Date
    • Thread