Re: Singleton instantiated more than once with -fvisibility=hidden

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



hi,

in my understanding a singleton is used to have a class
instanciated only for one time.

normally this is done by 

having the "wanted-to-be-singleton-class" derived/inherited from 
a singleton class or template.
never ever from the outside ctors are called, but there should be
a getInstance() method that checks for _m_instance == NULL
if it is matching creating a new instance with new, otherwise returning the _m_instance pointer.

if you want to, i can provide you with a complete singleton template.

regards
mws



On Friday 03 June 2005 14:39, Filipe Sousa wrote:
> On Friday 03 June 2005 11:29, Mws wrote:
> > imho this is not how to use a singleton at all.
> > think about what you do.
> 
> I don't see any problem, could you please be elaborate?
> 
> > On Friday 03 June 2005 12:25, Filipe Sousa wrote:
> > > I have 3 files, the library (singleton.h/.cc) and the main program
> > > (main.cc)
> > >
> > > -- singleton.h --
> > > #pragma once
> > >
> > > #include <iostream>
> > >
> > > class singleton {
> > > public:
> > >     singleton() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
> > >     static singleton* instance() {
> > >         static singleton s;
> > >         return &s;
> > >     }
> > > };
> > >
> > > -- singleton.cc --
> > > #include "singleton.h"
> > >
> > > void foo()
> > > {
> > >     singleton::instance();
> > > }
> > >
> > > -- main.cc --
> > > #include "singleton.h"
> > >
> > > int main() {
> > >     foo();
> > >     singleton::instance();
> > >     return 0;
> > > }
> > >
> > > c++ -g -fPIC -Wall -c singleton.cc -o singleton.o
> > > c++ -fPIC -shared -Wl,-soname,libsingleton.so -o libsingleton.so
> > > singleton.o c++ -Wall -g -c main.cc -o main.o
> > > c++ -g -fPIC main.o -o main -rdynamic -L${PWD} -lsingleton
> > > -Wl,-rpath,${PWD}
> > >
> > > fsousa@neptuno ~/tmp/vis $ ./main
> > > singleton::singleton()
> > >
> > > Here, the singleton is only instanciated once as expected, but if I add
> > > visibility support to the code
> > >
> > > -- singleton.h --
> > > #pragma once
> > >
> > > #include <iostream>
> > >
> > > class __attribute__ ((visibility("default"))) singleton {
> > > public:
> > >     singleton() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
> > >     static singleton* instance() {
> > >         static singleton s;
> > >         return &s;
> > >     }
> > > };
> > >
> > > __attribute__ ((visibility("default"))) void foo();
> > >
> > > c++ -g -fPIC -Wall -fvisibility-inlines-hidden -fvisibility=hidden -c
> > > singleton.cc -o singleton.o
> > > c++ -fPIC -fvisibility-inlines-hidden -fvisibility=hidden -shared
> > > -Wl,-soname,libsingleton.so -o libsingleton.so singleton.o
> > > c++ -Wall -g -c main.cc -o main.o
> > > c++ -g -fPIC main.o -o main -rdynamic -L${PWD} -lsingleton
> > > -Wl,-rpath,${PWD}
> > >
> > > fsousa@neptuno ~/tmp/vis $ ./main
> > > singleton::singleton()
> > > singleton::singleton()
> > >
> > > I have more than one instance. Am I doing something wrong?
> > >
> > > gcc (GCC) 4.0.1-beta20050526 (Gentoo 4.0.1_beta20050526)
> > >
> > > -- Filipe Sousa
> 

Attachment: pgpBL15kJjkly.pgp
Description: PGP signature


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux