On Mar 14, 2013, at 11:22 AM, Pax <45rpmli...@googlemail.com> 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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to