469,626 Members | 883 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

Singletons, Plugins, static and extern

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 Singleton* the();
private:
static Singleton* _instance;
}

//.cpp
Singleton* the(){
if(!_instance)
_instance=new Singleton;
return _instance;
}

There's no problem in my core application, which is statically linked
with the singleton classes, e.g. if I use Singleton::the()->xyz() in
main(), the object is created, and a later call uses this object as
expected.

The problem arises, if I try to use these Singletons in plugins / parts
of the program that are loaded with - in my case - dlopen(). It is
impossible for me to access a Singleton object, that was already created
in the main application from a plugin. There are 2 Singletons
afterwards, one in the scope of the plugin and one in the scope of the
main application.

Example:
main(){
//first call, create Singleton by accessing it
Singleton::the()->xxx();
Plugin* p=loadPluginWithDlopen();
p->yyy(); //Call to a virtual method of class Plugin
}

//ConcretePlugin is derived of plugin, yyy declared virtual
void ConcretePlugin::yyy(){
//Should use the same Singleton as above, but creates a new one.
Singleton::the()->zzz();
}

The question: How can I access the Singleton used in the core
application with the plugin? I guess that there's an "extern" missing
somewhere, but I can't solve it. Any suggestions?!

In case that this is important: I'm working with gcc version 3.3.5 on
Debian/GNU Linux(testing).

Thanks & regards,
Dominik
Jul 23 '05 #1
3 2925
Dominik Rau wrote:
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):
[...]
There's no problem in my core application, which is statically linked
with the singleton classes, e.g. if I use Singleton::the()->xyz() in
main(), the object is created, and a later call uses this object as
expected.

The problem arises, if I try to use these Singletons in plugins / parts
of the program that are loaded with - in my case - dlopen(). It is
impossible for me to access a Singleton object, that was already created
in the main application from a plugin.
Do you know *why* it is "impossible"? Since "plugins" or "dlopen" are not
part of the language, you're in the platform-specific area, and whatever
you have to do there cannot be solved using C++ means. You apparently
already wrote the program so that singletons are singletons, AFA C++ is
concerned.
There are 2 Singletons
afterwards, one in the scope of the plugin and one in the scope of the
main application.
That's not something that can be explained in terms of C++ language.

Example:
main(){
//first call, create Singleton by accessing it
Singleton::the()->xxx();
Plugin* p=loadPluginWithDlopen();
p->yyy(); //Call to a virtual method of class Plugin
}

//ConcretePlugin is derived of plugin, yyy declared virtual
void ConcretePlugin::yyy(){
//Should use the same Singleton as above, but creates a new one.
Singleton::the()->zzz();
}

The question: How can I access the Singleton used in the core
application with the plugin? I guess that there's an "extern" missing
somewhere, but I can't solve it. Any suggestions?!
Pass your singleton as an argument. It's not a solution, it's a work-
around.
In case that this is important: I'm working with gcc version 3.3.5 on
Debian/GNU Linux(testing).


It's not, really. (Not in comp.lang.c++, anyway)/

V
Jul 23 '05 #2
Dominik Rau wrote:
Hi.
I've got the following problem here: In my application, I use a lot of
Singletons,
This in itself COULD be a problem - google 'Singleton pattern' for a
very long conversation about their pros and cons.

To clarify my view of them....Singletons are not evil - but they are
often over-used and mis-used.
that are implemented as described in Gamma et al. (shortened):

//.h
class Singleton{
public:
static Singleton* the();
private:
static Singleton* _instance;
}

//.cpp
Singleton* the(){
if(!_instance)
_instance=new Singleton;
return _instance;
}
Minor point but 'the()' is not the usual name for the instance retrieval
method - getInstance() is.

There's no problem in my core application, which is statically linked
with the singleton classes, e.g. if I use Singleton::the()->xyz() in
main(), the object is created, and a later call uses this object as
expected.

The problem arises, if I try to use these Singletons in plugins / parts
of the program that are loaded with - in my case - dlopen(). It is
impossible for me to access a Singleton object, that was already created
in the main application from a plugin. There are 2 Singletons
afterwards, one in the scope of the plugin and one in the scope of the
main application.


Two birds with on stone here....to break your plugins use of the
Singleton (which is a good thing) pass a reference of the singleton, to
the plugin at load time. The plugin can then store/pass around the
reference in any manner it likes, but importantly, it doesn't know its a
singleton - its just another object.

snipped remaining
Jul 23 '05 #3
Hi again.
It works by linking the singleton classes as shared libraries with the
core application and the plugins. It was a linker problem and I
apologize for being completely offtopic here.

Thanks for your answers,
Dominik
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by ThunderMusic | last post: by
5 posts views Thread by stephan beal | last post: by
11 posts views Thread by Tito | last post: by
10 posts views Thread by Mark A. Gibbs | last post: by
5 posts views Thread by Omega | last post: by
14 posts views Thread by Jess | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.