troubleshooting Question

Static variable (Class) in the static function is getting re-initialized when called from another/shared Lib

Avatar of Ganesh N
Ganesh N asked on
C++
11 Comments1 Solution120 ViewsLast Modified:
Hi,
Facing the typical issue on the AIX compiler/linker/server.  Please check the code below.


//  say in module 1 I have class (libmod1.so)
======================================================================================
     in file  MyErrorHandlerSmptr.h
=======================================================================================       
class MyErrorHandlerSmptr
{

public:

    MyErrorHandlerSmptr (baseErrorHandler *p)
	{
		MyErrorHandler::instance().setErrorHandler(p);
	}

    ~MyErrorHandlerSmptr()
	{
	    MyErrorHandler::instance().setErrorHandler(NULL);      
	}

};
======================================================================================
======================================================================================
     in file  MyErrorHandler.h
=======================================================================================       
   
   
typedef enum
    {
        SUCCESS,      /**< Success */
        WARNINGS,     /**< Warning */
        ERRORS        /**< Error   */
    } MyErrorStatus;
	
class  MyErrorHandler
{
    friend class MyErrorHandlerSmptr;
	// 

private:
    MyErrorHandler()
	: errorHandler(NULL), errorStatus(SUCCESS), last_fail_value(0)
	{   
		printf("\n 11 :: default constructor ");	
		printf("\n address of the class is   --- > %p ", this );	
	}

    MyErrorHandler(const MyErrorHandler &)
	{
	 // code like constructor 
	}


    void setErrorHandler(BaseErrorHandler* handler)
	{
		printf("\n 33 : Set error handler");	
		printf("\n address of the class is   --- > %p ",(void *) this );

		errorHandler = handler;
		errorStatus = SUCCESS;
		last_fail_value = 100;

	}

    BaseErrorHandler* errorHandler;
    MyErrorStatus errorStatus;
    int last_fail_value;

public:
    static MyErrorHandler& instance()
    {
		printf("\n ## : in singleton instance");	
        static MyErrorHandler inst;
        return inst;
    }


    bool hasErrorHandler() const
    {
		printf("\n 44 : check error handler");	
		printf("\n address of the class is   --- > %p ",(void *) this );
        return errorHandler ? true:false;
    }
	
	~MyErrorHandler();
};

now When from Module 1. I initialize MyErrorHandlerSmptr(..) properly and MyErrorHandler class
works properly.

Now from another module say from module 2, When i check for error handler, like this.
bool ok_handler = MyErrorHandler::instance().hasErrorHandler();

Some how AIX calls the default constructor again from singleton instance function and saying no error handler is set.

Is there any solution, how to fix this?

Thanks in advance.
ASKER CERTIFIED SOLUTION
Ganesh N

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 11 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros