Singletons, Plugins, static and extern

D

Dominik Rau

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
 
V

Victor Bazarov

Dominik said:
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
 
A

Andrew McDonagh

Dominik said:
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
 
D

Dominik Rau

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top