Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shared Singleton across DLLs

Status
Not open for further replies.

globos

Programmer
Nov 8, 2000
260
FR
Hello,

I would like to have a Singleton class that ensures to have only one instance wherever the code flow is in a DLL or in the main program.
My Singleton class is like this:
Code:
template<class G>
class Singleton 
{
public:
  // Unique instance of type G.
  static G* instance ()
  {
    static G* _instance = Void;
    if (_instance == Void)
      _instance = new G;
    return _instance;
  }
};

My problem is that I have two different instances of a Singleton. One in my DLL and one in my main program.
In the main program :
Code:
int main ()
{
  Object* object = Singleton<Object>::instance ();
  cout << "address in main : " << object << endl;

  // Load the function test_in_dll() from the DLL and access the singleton inside...
  // Call the function :
  test_in_dll ();

  return 0;
}


void test_in_dll ()
{
  Object* object = Singleton<Object>::instance ();
  cout << "address in dll : " << object << endl;
}

It prints two different addresses.
Is it possible to have a real Singleton?

I'm using VC++ 6.0.

--
Globos
 
Hi,

You can use #pragma data_seg in your DLL to share the same instance of the singleton. Note that this will also share the same data across all applications that use the Dll.

You can google on #pragma data_seg.


Hope this helps.
Rich.



Programmers are tools for converting caffeine into code
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top