Thread: Static member func clone?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    30

    Static member func clone?

    Hi, i ran into a really weird problem:

    Take the following code:

    Code:
    class CExample{
            static CMemoryBuffer<CPlayerDescription>* Allocator()
            {
    	static CMemoryBuffer<CPlayerDescription>* instance = new CMemoryBuffer<CPlayerDescription>();
    	return instance;
            }
    };
    Now, i have another class within 2 member functions. Both functions do a call to CExample::Allocator to retrieve the allocator. However, weird enough they both got their own instances.

    Is this normal behaviour? because the method is static, shouldn't it return the same instance?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Can you post a small but complete and compilable example that shows the problem?

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    30
    Hi daved:

    This is what i do: (Couldnt copy/paste the original code but the structure is exactly the same

    Code:
    class CShared{
    	void doSomething() { } 
    };
    
    class CFoo{
    public:
    	static CShared* get_SharedInstance(){
    		static CShared* pObjShared = new CShared();
    		return pObjShared;
    	}
    };
    
    class CBar{
    	void Method1{
    		CShared* pObjShared = CFoo::get_SharedInstance();
    		std::cout << pObjShared << std::endl;
    	}
    	void Method2{
    		CShared* pObjShared = CFoo::get_SharedInstance();
    		std::cout << pObjShared << std::endl;
    	}
    };
    Method 1 prints: address1
    Method 2 prints: address2

    The application is multithreaded, better said, there is little change that the same thread who called method 1, calls method 2 on the same instance. Might this have something to do with it?

    I use singleton classes alot in my application ( yeh i know ugly ) so is surely hope that this problem doesn't exists in those classes.

    I solved it temporary by using a funvtion returning that object which works fine

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I was kind of hoping for something that I could plop into my compiler, build it and run it and see the same results. I was able to make some changes to that code and get it to compile, but it gave the same value for both methods (VC++ 7.1).

    Part of reducing the problem to a complete and compilable example is that it might help you narrow down the issue. The multithreaded-ness of the app might have something to do with it, I'm not sure. If you can make a small sample that works in a singlethreaded system but not a multithreaded system, that would be a good indicator. Regardless, unless somebody knows from experience what the issue is, finding the smaller example might be the best course of action.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    88
    >Is this normal behaviour? because the method is static, shouldn't it return the same instance?

    The pointer itself is static but the address assigned to it is not. Try statically allocating the CMemoryBuffer object itself instead of using new and see what happens.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    But a static pointer should only be initialized once, so new should only be called once.

    Another option is to put a breakpoint on that line and see from where and when it gets called.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. Linking errors with static var
    By Elysia in forum C++ Programming
    Replies: 8
    Last Post: 10-27-2007, 05:24 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Static member functions more efficient?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2006, 07:07 AM