C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Singleton without statics?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Paul Rosen
Guest





PostPosted: Sat Nov 19, 2005 10:24 am    Post subject: Singleton without statics? Reply with quote



I need to implement a singleton, but I can't use static data for the
following reason:

Our application (on Windows) consists of a small EXE and a number of
DLLs. We also have some static libraries that are linked in to the EXE
or a DLL as needed.

We have a singleton that resides in one of the DLLs. The singleton is
similar to the "Meyers singleton" that Modern C++ Design mentions:

Singleton& Singleton::Instance()
{
static Singleton obj;
return obj;
}

This works fine because there is one instance of the above routine in
the system and it physically resides in the DLL.

---

We recently tried to recompile that particular DLL as a static lib and
discovered that we got different singletons depending on which DLL was
accessing it. That's because if a DLL referenced the
Singleton::Instance() function, a copy of it is placed in the DLL during
the link phase. If two DLL reference it, each would get a copy of the
function, and hence, two copies of the static object.

---

I don't see how to implement this differently. Somehow every DLL needs
to point to the same copy of the singleton, so there must be some public
variable somewhere. But if there is, how could the linker decide where
to put it, since the DLLs are linked separately?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Pete Becker
Guest





PostPosted: Sat Nov 19, 2005 4:13 pm    Post subject: Re: Singleton without statics? Reply with quote



Paul Rosen wrote:

Quote:

I don't see how to implement this differently. Somehow every DLL needs
to point to the same copy of the singleton, so there must be some public
variable somewhere. But if there is, how could the linker decide where
to put it, since the DLLs are linked separately?


Your original design was correct.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Alberto Ganesh Barbati
Guest





PostPosted: Mon Nov 21, 2005 9:48 am    Post subject: Re: Singleton without statics? Reply with quote



Paul Rosen wrote:
Quote:
I don't see how to implement this differently. Somehow every DLL needs
to point to the same copy of the singleton, so there must be some public
variable somewhere. But if there is, how could the linker decide where
to put it, since the DLLs are linked separately?

DLLs and shared libraries are topics that are not standardized, so your
solution does not have a clean-cut *standard* solution. In fact the only
standard solution is to ensure that the function creating the singleton
is present in one and only one DLL and that all other DLLs refer to that
one function. The best thing is to have it in a separate DLL (i.e.: your
original design) or alternatively to have it into some other DLL that
must be present for the application to work.

As a last resort, but here we are going into the non-portable and
slightly going off-topic, you can ask the OS to allocate some
application-global memory where to store the object or at least a
pointer to it. One such way could be via named memory-mapping objects.

HTH,

Ganesh

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Keith A. Lewis
Guest





PostPosted: Mon Nov 21, 2005 10:33 am    Post subject: Re: Singleton without statics? Reply with quote

In article <gSrff.10097$2y.1013 (AT) newsread2 (DOT) news.atl.earthlink.net>,
Paul Rosen <prosen (AT) fte (DOT) com> wrote:
Quote:
I need to implement a singleton, but I can't use static data for the
following reason:

Our application (on Windows) consists of a small EXE and a number of
DLLs. We also have some static libraries that are linked in to the EXE
or a DLL as needed.

We have a singleton that resides in one of the DLLs. The singleton is
similar to the "Meyers singleton" that Modern C++ Design mentions:

Singleton& Singleton::Instance()
{
static Singleton obj;
return obj;
}

This works fine because there is one instance of the above routine in
the system and it physically resides in the DLL.

---

We recently tried to recompile that particular DLL as a static lib and
discovered that we got different singletons depending on which DLL was
accessing it. That's because if a DLL referenced the
Singleton::Instance() function, a copy of it is placed in the DLL during
the link phase. If two DLL reference it, each would get a copy of the
function, and hence, two copies of the static object.

---

I don't see how to implement this differently. Somehow every DLL needs
to point to the same copy of the singleton, so there must be some public
variable somewhere. But if there is, how could the linker decide where
to put it, since the DLLs are linked separately?

If you are using MSVC++ you can use something like:

// shared by all dll instances
#pragma data_seg(".SHARED")

char buf[256] = {0};

#pragma data_seg()

#pragma comment(linker, "/section:.SHARED,RWS")

All dlls will look at the same buf[] array.

Another handy (nonportable) trick is to use

#pragma init_seg(lib)

in a module to force global variables to be constructed before
those in "user" segments.
--

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Eugene Alterman
Guest





PostPosted: Thu Nov 24, 2005 8:06 am    Post subject: Re: Singleton without statics? Reply with quote

"Keith A. Lewis" <kal (AT) panix (DOT) com> wrote

Quote:
In article <gSrff.10097$2y.1013 (AT) newsread2 (DOT) news.atl.earthlink.net>,
Paul Rosen <prosen (AT) fte (DOT) com> wrote:
[snip]
We recently tried to recompile that particular DLL as a static lib and
discovered that we got different singletons depending on which DLL was
accessing it. That's because if a DLL referenced the
Singleton::Instance() function, a copy of it is placed in the DLL during
the link phase. If two DLL reference it, each would get a copy of the
function, and hence, two copies of the static object.

---

I don't see how to implement this differently. Somehow every DLL needs
to point to the same copy of the singleton, so there must be some public
variable somewhere. But if there is, how could the linker decide where
to put it, since the DLLs are linked separately?

If you are using MSVC++ you can use something like:

// shared by all dll instances
#pragma data_seg(".SHARED")

char buf[256] = {0};

#pragma data_seg()

#pragma comment(linker, "/section:.SHARED,RWS")

All dlls will look at the same buf[] array.

This is done to share data statically allocated in a DLL among different
processes that load that DLL.
It has nothing to do with the OP problem where a C++ object needs to be
shared by modules within the same process.
C++ objects cannot be generally shared by different processes.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Peter Kümmel
Guest





PostPosted: Thu Nov 24, 2005 5:16 pm    Post subject: Re: Singleton without statics? Reply with quote


Alberto Ganesh Barbati schrieb:
Quote:
DLLs and shared libraries are topics that are not standardized, so your
solution does not have a clean-cut *standard* solution. In fact the only
standard solution is to ensure that the function creating the singleton
is present in one and only one DLL and that all other DLLs refer to that
one function. The best thing is to have it in a separate DLL (i.e.: your
original design) or alternatively to have it into some other DLL that
must be present for the application to work.

As a last resort, but here we are going into the non-portable and
slightly going off-topic, you can ask the OS to allocate some
application-global memory where to store the object or at least a
pointer to it. One such way could be via named memory-mapping objects.

When using a Dll you are allways on a windows system, so I could
imagine
that an other non portable solution is to use the registry.

A portable solution is maybe to create a log-file for the singleton
which the
singletons have to check if they are going to create a new object.
But this could maybe only prevent dupicated singletons

Peter


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.