Multiple instantiation of template Singleton in multiple so instances

Source: Internet
Author: User

During the android package project, the login function was unavailable, and no problems were found in logcat. The problem was found in the last line of log. It turns out that the constructor in the so file is initialized twice!

 

This Singleton is implemented by inheriting the template (thread security issues are not considered for the moment)

template<class T>
class CSingleT
{
public:
static T * Instance()
{
if (!ms_pObject)
{
ms_pObject = new T;
}
return ms_pObject;
}

static void Create()
{
if (!ms_pObject)
{
ms_pObject = new T;
}
}

static void Destroy()
{
if (ms_pObject)
{
delete ms_pObject;
ms_pObject = NULL;
}
}

static T * Get()
{
return ms_pObject;
}

static void Reset()
{
Destroy();
Create();
}

protected:
static T * ms_pObject;
};

template <class T>
T * CSingleT<T>::ms_pObject = NULL;

The main thread of the game is to directly call the instance () method, and then pass. in so, a static method is used to call instance (). In fact, the result is that the second singleton object will be initialized after the static method is called directly.

 

At present, the temporary solution is to call the instance method in the main thread by calling the. So static method, so that only one instance object will be generated. This does not involve the problem of multithreading, so there is no full code for thread security.

 

Using static methods and then calling the instance object is indeed a bad method. This is done for the time being to make the game run.

 

Refer:

Multiple instances (Linux) appear in single-instance mode between dynamic libraries)

Cross-So (DLL) Problem of template Singleton in C ++: rtti, typeid, static, Singleton

Multiple instantiation of template Singleton in multiple so instances

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.