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

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

 



imho this is not how to use a singleton at all.
think about what you do.



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: pgpVRmxafoqDv.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