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

  • Home
  • SEARCH
  • 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 584055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:51:29+00:00 2026-05-13T14:51:29+00:00

My method + (void) initialized is not called and I’m very new in Objective

  • 0

My method + (void) initialized is not called and I’m very new in Objective C. The code is in the book iPhone Game Development and I have to call the method explicitly to work. The code in the .m file is that:

ResourceManager *g_ResManager;

@implementation ResourceManager

//initialize is called automatically before the class gets any other message, per from http://stackoverflow.com/questions/145154/what-does-your-objective-c-singleton-look-like
+ (void) initialize
{
    static BOOL initialized = NO;
    if(!initialized)
    {
        initialized = YES;
        g_ResManager = [[ResourceManager alloc] init];
    }
}

...

@end

But in the .h file a external declaration of the variable is made:

extern ResourceManager *g_ResManager; //paul <3's camel caps, hungarian notation, and underscores.

@interface ResourceManager : NSObject {
   ...
}
...
@end

I tried everything (remove the external, put static in the .m declaration) and always get compilation errors. The code above compiles but the method initialize is never called (putted a breakpoint to see that).

Some clue?

  • 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-05-13T14:51:29+00:00Added an answer on May 13, 2026 at 2:51 pm

    +initialize is not called until you send some message to an instance of the class. Did you send a message?

    One possible problem might be that you sent a message to g_ResManager from another portion of your code?
    That won’t work, because:

    1. g_ResManager is nil at the launch time.
    2. You send a message to g_ResManager, which is nil.
    3. What Objective-C runtime counts as “sending a message to a class” is not what it looks syntactically in the source code, but the real object and the message sent.
    4. So, in this case, nil gets the message, nil is not an instance of ResourceManager, so +initialize is not called either.

    I would change your code as follows: first, in .m,

    static ResourceManager *g_ResManager;
    
    @implementation ResourceManager
    
    //initialize is called automatically before the class gets any other message
    + (void) initialize
    {
        static BOOL initialized = NO;
        if(!initialized)
        {
            initialized = YES;
            g_ResManager = [[ResourceManager alloc] init];
        }
    }
    +(ResourceManager*)sharedResourceManager
    {
         return g_ResManager;
    }
    ...
    
    @end
    

    and then in .h, I would just have

    @interface ResourceManager:NSObject {
    ...
    }
    +(ResourceManager*)sharedResourceManager
    @end
    

    Then, you can always use [ResourceManager sharedResourceManager].

    In fact, as Rob says in the comment, you can totally do away with +initialize in this case. Change .m to something like

    @implementation ResourceManager
    
    +(ResourceManager*)sharedResourceManager
    {
         static ResourceManager *g_ResManager=nil;
         if(!g_ResManager){
             g_ResManager=[[ResourceManager alloc] init];
         }
         return g_ResManager;
    }
    ...
    
    @end
    

    This is the idiom I always use personally. But I warn you this is not completely thread safe! It should be OK as long as you call [ResourceManager sharedResourceManager] once before spawning threads, which I would almost always do anyway, but that’s one thing to keep in mind. On the other hand, the version above using +initialize should be thread safe as is, thanks to the well-defined behavior of +initialize. See discussions in this blog post.

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

Sidebar

Related Questions

When my app is run in the iPhone simulator, the delegate method - (void)applicationWillTerminate:(UIApplication
I have an interface method public void Execute(ICommand command); which needs to pass known
I have this method: private delegate void watcherReader(StreamReader sr); private void watchProc(StreamReader sr) {
I have the following C++ method : __declspec(dllexport) void __stdcall getDoubles(int *count, double **values);
If I have a method such as: public void MyMethod(int arg1, string arg2) How
I have a method which takes params object[] such as: void Foo(params object[] items)
If I have a method such as: private function testMethod(param:string):void { // Get the
I have a C++ method signature that looks like this: static extern void ImageProcessing(
Say instead of returning void a method you returned a reference to the class
The method signature of a Java main method is: public static void main(String[] args)

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.