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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:51:03+00:00 2026-06-05T04:51:03+00:00

Consider this code: +(id)sharedInstance { static dispatch_once_t pred; static MyClass *sharedInstance = nil; dispatch_once(&pred,

  • 0

Consider this code:

+(id)sharedInstance
{
    static dispatch_once_t pred;
    static MyClass *sharedInstance = nil;
    dispatch_once(&pred, ^{
        sharedInstance = [[MyClass alloc] init];
    });
    return sharedInstance;
}

If I follow this singleton design pattern I can make the following assumptions:

  • The allocation and initialization will only be executed once thanks
    to GCD.
  • The sharedInstance class variable can only be accessed from
    within this implementation and shared among the class regardless of the instance.

First time I create the instance I would do something like:

MyClass *something = [MyClass sharedInstance];

my question is, If I call the previews code again but like this:

MyClass *somethingOther = [MyClass sharedInstance];

I can only think of one outcome.

Outcome:

static MyClass *sharedInstance = nil;

Makes sharedInstance class variable point to nil and a nil is returned so somethingOther will be nil.

But I thought that what was supposed to happen in a singleton is that the shared instance would be returned instead.

Now consider this code:

+ (MotionManagerSingleton*)sharedInstance {

    static MotionManagerSingleton *_sharedInstance;
    if(!_sharedInstance) {
        static dispatch_once_t oncePredicate;
        dispatch_once(&oncePredicate, ^{
            _sharedInstance = [[super allocWithZone:nil] init];
            });
    }

    return _sharedInstance;
}

+ (id)allocWithZone:(NSZone *)zone {    

    return [self sharedInstance];
}

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

Here the

static MotionManagerSingleton *_sharedInstance;

Doesnt set my variable to nil, but i thought that all object pointers are initialized to nil by default.

My question is, how are these class methods returning the “sharedInstance”?

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-05T04:51:04+00:00Added an answer on June 5, 2026 at 4:51 am

    One. Non-initialized pointers are non-initialized.

    static MotionManagerSingleton *_sharedInstance;
    

    won’t make your MotionManagerSingleton point to nil. It will point to an undefined (garbage) location.

    Two. Variables declared static are initialized only once (yes, the syntax is a bit inconsistent with the semantics), so your first implementation won’t null out the returned shared instance. That’s a perfectly fine implementation.

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

Sidebar

Related Questions

Consider this code: public <T> List<T> meth(List<?> type) { System.out.println(type); // 1 return new
Consider this code: int main() { int e; prn(e); return 0; } void prn(double
Consider this code : class MyClass<T> { } class AnotherClass : MyClass<String> { }
Consider this code: const char* someFun() { // ... some stuff return Some text!!
Consider this code snippet: public static class ApplicationContext { private static Func<TService> Uninitialized<TService>() {
Consider this code: public static class PredefinedStrings { public const string Golf = Golfing;
Consider this code: using namespace std; int* get() { unique_ptr<int> p (new int[4]); return
Consider this code snippet public class ConstantFolding { static final int number1 = 5;
Consider this code: public class DummyClass { public List<? extends Number> dummyMethod() { return
Consider this code : class App { public static function log($msg) { echo $msg;

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.