Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9231301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:01:54+00:00 2026-06-18T06:01:54+00:00

I have this singleton code in an app, which I update now and then.

  • 0

I have this singleton code in an app, which I update now and then. While I took the Apple reference code, I’ve had to update it once or twice since clang was complaining and Apple updated it in response. Today I tried analyzing with Xcode 4.6 and I get a warning again, although Apple hasn’t updated in two years.
I really can’t understand how they can’t get a singleton coded in a way that static analyzers will not complain. And it’s not like they can’t change the static analyzer to accommodate their code if they feel their reference code is right.
But back to the code, I have this class:

@implementation MySingleton

static MySingleton *sSharedSingleton = nil;

+ (MySingleton *)sharedSingleton 
{
    if (sSharedSingleton == nil)
    {
        sSharedSingleton = [[super allocWithZone:NULL] init];
    }
    return sSharedSingleton;    
}

+ (id)allocWithZone:(NSZone *)zone
{
    return [[self sharedSingleton] retain];
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

- (id)retain
{
    return self;
}

- (unsigned)retainCount
{
    return NSUIntegerMax;  //denotes an object that cannot be released
}

- (oneway void)release
{
    //do nothing
}

- (id)autorelease
{
    return self;
}

@end

As far as I can tell, it is exactly the reference from https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html

Clang is complaining about the allocWithZone implementation’s return line with the warning:

Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected

I assume we get that because we are overwriting retain. Who do you think is correct, clang or the reference code? Should I just change the code to suppress the message (e.g. avoid overwriting retain) and forget about it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:01:56+00:00Added an answer on June 18, 2026 at 6:01 am

    That example is outdated, especially so with introduction of ARC and GCD, mostly, you only need this

    + (DSSingleton *)sharedInstance
    {
        static DSSingleton *sharedInstance;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            sharedInstance = [DSSingleton new];
        });
    
        return sharedInstance;
    }
    

    Xcode has great code snippet for that inner part – just type dispatch_once and look for autocomplete

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a singleton which once hit will load user profile information, I want
I have a singleton class in my play app. This singleton class is a
I have a singleton class for global access to config information. This singleton class
I have a class that is a singleton. This singleton is instantiated using GCD
I have a singleton implemented like this: class Test123(object): _instance = None def __new__(cls,
Looking at this article: http://www.galloway.me.uk/tutorials/singleton-classes/ I have implemented a singleton, my question is the
I have a question, is this the correct approach to make a Generic Singleton?
I have an app which pretty much follows the method described here . The
I have a singleton in my FTP app designed to store all of the
I have a singleton delayed-transaction manager class, which is responsible for enqueueing and dequeueing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.