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 8045881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:39:44+00:00 2026-06-05T05:39:44+00:00

In your opinion if I have a singleton subclass of NSObject being initialised with

  • 0

In your opinion if I have a singleton subclass of NSObject being initialised with parameters like this:

- (MyObject *) initWithSomeParam:(NSString *)param{
    self = [super init];
    if (SharedInstance == nil){
        SharedInstance = [super init];
        SharedInstance.someProperty = param;
    }
    return self;
}

+ (MyObject *) objectWithSomeParam:(NSString *)param{
    return [[self alloc] initWithSomeParam:param];
    // Will the alloc cause a leak?
}

The user doesn’t have access to the instance method, just the class. Thanks.

  • 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-05T05:39:45+00:00Added an answer on June 5, 2026 at 5:39 am

    That’s not the normal way of implementing a singleton and you are breaking the convention of init. Better would be to create a sharedInstance class method and leave the initWithParam method to be more conventional:

    static MyObject *_sharedInstance = nil;
    
    + (MyObject *)sharedInstance:(NSString *)param
    {
        if (_sharedInstance == nil)
        {
            _sharedInstance = [MyObject alloc] initWithParam:param];
        }
        return _sharedInstance;
    }
    
    // This must be called during app termination to avoid memory leak
    + (void)cleanup
    {
        [_sharedInstance release];
        _sharedInstance = nil;
    }
    
    - (id)initWithParam:(NSString *)param
    {
        self = [super init];
        if (self != nil)
        {
            self.someProperty = param;
        }
        return self;
    }
    

    However, even that doesn’t seem very comfortable; i.e. what happens if the user calls sharedInstance with a different parameter? Perhaps you want to keep a NSMutableDictionary of the initialized objects and create/return them depending on the parameter?

    If so, you would do:

    static NSMutableDictionary _sharedInstances = [[NSMutableDictionary alloc] init];
    
    + (MyObject *)sharedInstance:(NSString *)param
    {
        MyObject *obj = [_sharedInstances objectForKey:param];
        if (obj == nil)
        {
            obj = [[MyObject alloc] initWithParam:param];
            [_sharedInstances setObject:obj forKey:param];
        }
        return obj;
    }
    
    // This must be called during app termination to avoid memory leak
    + (void)cleanup
    {
        [_sharedInstances release];
        _sharedInstances = nil;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to have your opinion on a specific case, please. This is about
I would like to have your opinion and suggestion regarding the solution to this
I have a little experience with WCF and would like to get your opinion/suggestion
I'd like to ask your opinion on this. This is a theoretical question. I'm
i would like to have your opinion in a project i am currently working
I'm quite sure with this, but just to have your opinion: Is it somehow
I want to get your opinion on this. I have a class which is
I need your opinion about this code below. I have a list of messages:
I would like to have your opinion about the subject version control, but focusing
I would like to have your opinion on a simple regular expression I built

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.