473,473 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to use Singletons over dll boundaries?

1 New Member
Hi all,

actually i migrate our Linux sources to windows using MSYS/MinGW. Now I've to migrate several global utility singletons (template based) , which are placed in a shared library, everything is working fine with Linux and in context of the dll.

The singleton:
Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2.  
  3. class brSingleton
  4. {
  5. public:
  6.     static T& getInstance(){
  7.         if(m_instance == 0)
  8.             m_instance = new T;
  9.  
  10.         return *m_instance;
  11.     }
  12.  
  13.     virtual void delInstance(){
  14.         delete(m_instance);
  15.         m_instance = 0;
  16.     }
  17.  
  18. protected:
  19.     brSingleton() {}
  20.     virtual ~brSingleton()  {}
  21.  
  22. private:
  23.     brSingleton(const brSingleton& s) {}
  24.     const brSingleton& operator=(const brSingleton& s) const {}
  25.     brSingleton& operator=(const brSingleton& s) {}
  26.  
  27.     static T*    m_instance;
  28. };
  29.  
  30. template <class T>
  31. T* brSingleton<T>::m_instance = 0;
  32.  
But I'm getting errors when I try to use it from several different dll's, while got different singleton instances in those dll's. I tried to use the windows dllimport/export macros but i fail.

The import/export macros in brCommons.h
Expand|Select|Wrap|Line Numbers
  1. #ifdef WIN32
  2.   #ifdef MYLIB_EXPORTS
  3.     #define MYLIB_API __declspec(dllexport)
  4.   #else
  5.     #define MYLIB_API __declspec(dllimport)
  6.   #endif
  7. #else
  8.   //define empty values for linux OS
  9.   #define MYLIB_API
  10. #endif
  11.  
The concrete singleton header:
Expand|Select|Wrap|Line Numbers
  1. class MYLIB_API brConfiguration : public brCore::brSingleton<brConfiguration>
  2. {
  3.     friend class brCore::brSingleton<brConfiguration>;
  4.  
  5. public:
  6.     enum Types {
  7.         INT,    // 0
  8.         LONG,   // 1
  9.         UINT,   // 2
  10.         ULONG,  // 3
  11.         FLOAT,  // 4
  12.         DOUBLE, // 5
  13.         STRING, // 6
  14.         CHAR,   // 7
  15.         ANY     // 8
  16.     };
  17.  
  18.     struct valuetype{
  19.         brAnyType value;
  20.         Types type;
  21.     };
  22.  
  23.    void setValue(std::string key, const int& value);
  24.    void setValue(std::string key, const long& value);
  25.    void setValue(std::string key, const unsigned int& value);
  26.    void setValue(std::string key, const unsigned long& value);
  27.    void setValue(std::string key, const float& value);
  28.    void setValue(std::string key, const double& value);
  29.    void setValue(std::string key, const std::string& value);
  30.    void setValue(std::string key, const char* value);
  31.    void setAnyValue(std::string key, brAnyType value);
  32.  
  33.    // handling lex_t uses.
  34.    brAnyType getValue(std::string key, brAnyType default_value = 0);
  35.  
  36.    valuetype getInternalValue(std::string key);
  37.  
  38. protected:
  39.    brConfiguration();
  40.    virtual ~brConfiguration();
  41.    void setValue(std::string key, brAnyType value, Types type);
  42.  
  43. private:
  44.    typedef std::map<std::string,valuetype> MapT;
  45.    MapT m_configs;
  46. };
  47.  
And the concrete singleton cpp module header to force an dll export and template export:

Expand|Select|Wrap|Line Numbers
  1. // Ensure that the dll hader will be exported
  2. #define MYLIB_EXPORTS
  3. #include <brConfig/brConfiguration.h>
  4.  
  5. // You must also export the template instantiation Singleton< MyClass > into 
  6. // the DLL you are compiling. That should be it!
  7. template MYLIB_API class binrev::brCore::brSingleton<brConfiguration>; 
  8.  
Knows anyone a solution?
I'm thankfully for any hint or point in the right direction.

Best regards,
Chris

P.S.: You could get the complete test source here: http://sourceforge.net/projects/binr...st.7z/download

I've included two executables in the lib subfolder demonstrating the problem. The ConfigTest.exe shows that the singleton works fine in dll context, the SingletonTest.exe demonstrating the problem using te singleton across differend dll's.
Sep 10 '10 #1
1 10205
weaknessforcats
9,208 Recognized Expert Moderator Expert
It does not appear that the singleton is set up correctly.

Using multiple dlls should njot be a problem. Using multiple threads will be an issue since the code is not thread safe.

The singleton needs a global point of acess and there needs to be exactly one singleton instance for the entire progem regardless of the number of dlls.

I also believe there is a design weakness due to the use of friend classes. A friend class breaks encapsulation and that's a no-no.

Read this: http://bytes.com/topic/c/insights/65...erns-singleton
Sep 10 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Angelos Karantzalis | last post by:
Hi y'all ... I'm a bit puzzled here about .NET class instancing under COM+ Issue 1: I've a COM+ component, let's call it ... COMDbWrapper that initializes itself from an xml file. The data...
5
by: stephan beal | last post by:
Good morning, C++ users, i've been hesitating to post this, primarily because i know that most of you here are *way* ahead of me in C++ and i'm a little embarassed about the possibility of some...
11
by: Tito | last post by:
I have two questions about the singletons' chapter of Alexei Alexandrescu's "C++ Modern Design". 1. In the beginning of the chapter Alexei states that a "singleton" class implementation made of...
3
by: Dominik Rau | last post by:
Hi. I've got the following problem here: In my application, I use a lot of Singletons, that are implemented as described in Gamma et al. (shortened): //.h class Singleton{ public: static...
11
by: John Fly | last post by:
I'm working on a large project(from scratch). The program is essentially a data file processor, the overall view is this: A data file is read in, validated and stored in a memory structure...
6
by: Steven Watanabe | last post by:
PEP 8 says, "Comparisons to singletons like None should always be done with 'is' or 'is not', never the equality operators." I know that "is" is an identity operator, "==" and "!=" are the equality...
5
by: Omega | last post by:
I'm interested in seeing a bit of discussion about using singletons in ASP.NET 2.0. Currently I've designed a singleton that gets a reference to it's single instance stored inside the ASP.NET...
6
by: =?Utf-8?B?R29yZG8=?= | last post by:
Hello everyone, I've been trying for some time now to move to C++/CLI, but I have several large legacy C++ static libraries I need to use. When I set up a simple solution with a C++/CLI Winforms...
7
by: adam.timberlake | last post by:
I was reading an article on TalkPHP (http://www.talkphp.com/ showthread.php?t=1304) about singletons but I'm afraid I don't understand why I need to use them. I understand how to code them...
12
by: Craig Allen | last post by:
Hey, forgive me for just diving in, but I have a question I was thinking of asking on another list but it really is a general question so let me ask it here. It's about how to approach making...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.