public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Singleton instantiated more than once with -fvisibility=hidden
@ 2005-06-03 10:25 Filipe Sousa
  2005-06-03 10:29 ` Mws
  0 siblings, 1 reply; 11+ messages in thread
From: Filipe Sousa @ 2005-06-03 10:25 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1871 bytes --]

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 #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Singleton instantiated more than once with -fvisibility=hidden
  2005-06-03 10:25 Singleton instantiated more than once with -fvisibility=hidden Filipe Sousa
@ 2005-06-03 10:29 ` Mws
  2005-06-03 12:40   ` Filipe Sousa
  0 siblings, 1 reply; 11+ messages in thread
From: Mws @ 2005-06-03 10:29 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 2161 bytes --]


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 #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Singleton instantiated more than once with -fvisibility=hidden
  2005-06-03 10:29 ` Mws
@ 2005-06-03 12:40   ` Filipe Sousa
  2005-06-03 12:46     ` Mws
  0 siblings, 1 reply; 11+ messages in thread
From: Filipe Sousa @ 2005-06-03 12:40 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 2422 bytes --]

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

-- 
Filipe Sousa

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Singleton instantiated more than once with -fvisibility=hidden
  2005-06-03 12:40   ` Filipe Sousa
@ 2005-06-03 12:46     ` Mws
  2005-06-03 13:09       ` Filipe Sousa
  0 siblings, 1 reply; 11+ messages in thread
From: Mws @ 2005-06-03 12:46 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 3175 bytes --]

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 #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Singleton instantiated more than once with -fvisibility=hidden
  2005-06-03 12:46     ` Mws
@ 2005-06-03 13:09       ` Filipe Sousa
  2005-06-03 13:22         ` Mws
  0 siblings, 1 reply; 11+ messages in thread
From: Filipe Sousa @ 2005-06-03 13:09 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1110 bytes --]

On Friday 03 June 2005 13:46, Mws wrote:
> 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.

A templated version

template<class T>
class singleton {
public:
    static T* instance() {
        if (!m_instance)
            m_instance = new T;
        return m_instance;
    }

private:
    static  __attribute__ ((visibility("default"))) T* m_instance;
};

class __attribute__ ((visibility("default"))) my_singleton : public 
singleton<my_singleton> {
public:
    my_singleton() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
};

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

Yes, please

> regards
> mws


-- 
Filipe Sousa

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Singleton instantiated more than once with -fvisibility=hidden
  2005-06-03 13:09       ` Filipe Sousa
@ 2005-06-03 13:22         ` Mws
  2005-06-03 13:45           ` Filipe Sousa
  0 siblings, 1 reply; 11+ messages in thread
From: Mws @ 2005-06-03 13:22 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 2383 bytes --]

On Friday 03 June 2005 15:09, Filipe Sousa wrote:
> On Friday 03 June 2005 13:46, Mws wrote:
> > 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.
> 
> A templated version
> 
> template<class T>
> class singleton {
> public:
>     static T* instance() {
>         if (!m_instance)
>             m_instance = new T;
>         return m_instance;
>     }
> 
> private:
>     static  __attribute__ ((visibility("default"))) T* m_instance;
> };
> 
> class __attribute__ ((visibility("default"))) my_singleton : public 
> singleton<my_singleton> {
> public:
>     my_singleton() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
> };
> 
> > if you want to, i can provide you with a complete singleton template.
> 
> Yes, please
> 
> > regards
> > mws
> 
> 

singleton.h

#ifndef __singleton__
#define __singleton__

template<class IMPLEMENTINGCLASS>
class TSingleton
{
	private:
		static IMPLEMENTINGCLASS* instance;

	public:
		static IMPLEMENTINGCLASS* getInstance()
		{
			//debugging purposes
			std::cout << "before" << instance << std::endl;
			if(!instance)
			{
				instance = new IMPLEMENTINGCLASS();
			}
			std::cout << "after" << instance << std::endl;
			return instance;
		}

		void dispose()
		{
			if(instance)
			{
				delete instance;
				instance = NULL;
			}
		}
};

template<class IMPLEMENTINGCLASS> IMPLEMENTINGCLASS* TSingleton<IMPLEMENTINGCLASS>::instance = NULL;

#endif

usage is like

class CFoo:public TSingleton<foo>
{
	private:
		CFoo();
		virtual ~CFoo();

	public:

		...
		...

};

in case of debugging this you should do a std::cout << instance << std::endl;
instead of mentioning that the ctor is called.


int main() {
    // get the instance 
    CFoo::getInstance();
    CFoo::getInstance();

    // free mem - call dtor
    CFoo::dispose(); 

    return 0;
}

that's what i would do

regards
mws


[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Singleton instantiated more than once with -fvisibility=hidden
  2005-06-03 13:22         ` Mws
@ 2005-06-03 13:45           ` Filipe Sousa
  2005-06-03 14:09             ` Mws
  0 siblings, 1 reply; 11+ messages in thread
From: Filipe Sousa @ 2005-06-03 13:45 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1243 bytes --]

On Friday 03 June 2005 14:22, Mws wrote:
> template<class IMPLEMENTINGCLASS>
> class TSingleton
> {
>         private:
>                 static IMPLEMENTINGCLASS* instance;
>
>         public:
>                 static IMPLEMENTINGCLASS* getInstance()
>                 {
>                         //debugging purposes
>                         std::cout << "before" << instance << std::endl;
>                         if(!instance)
>                         {
>                                 instance = new IMPLEMENTINGCLASS();
>                         }
>                         std::cout << "after" << instance << std::endl;
>                         return instance;
>                 }
>
>                 void dispose()
>                 {
>                         if(instance)
>                         {
>                                 delete instance;
>                                 instance = NULL;
>                         }
>                 }
> };
>
> template<class IMPLEMENTINGCLASS> IMPLEMENTINGCLASS*
> TSingleton<IMPLEMENTINGCLASS>::instance = NULL;

But I still have more then one instance:

before0
my_singleton::my_singleton()
after0x804a008
before0
my_singleton::my_singleton()
after0x804a018

-- 
Filipe Sousa

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Singleton instantiated more than once with -fvisibility=hidden
  2005-06-03 13:45           ` Filipe Sousa
@ 2005-06-03 14:09             ` Mws
  2005-06-03 14:20               ` Filipe Sousa
  0 siblings, 1 reply; 11+ messages in thread
From: Mws @ 2005-06-03 14:09 UTC (permalink / raw)
  To: gcc-help

On Friday 03 June 2005 15:45, Filipe Sousa wrote:
> On Friday 03 June 2005 14:22, Mws wrote:
> > template<class IMPLEMENTINGCLASS>
> > class TSingleton
> > {
> >         private:
> >                 static IMPLEMENTINGCLASS* instance;
> >
> >         public:
> >                 static IMPLEMENTINGCLASS* getInstance()
> >                 {
> >                         //debugging purposes
> >                         std::cout << "before" << instance << std::endl;
> >                         if(!instance)
> >                         {
> >                                 instance = new IMPLEMENTINGCLASS();
> >                         }
> >                         std::cout << "after" << instance << std::endl;
> >                         return instance;
> >                 }
> >
> >                 void dispose()
> >                 {
> >                         if(instance)
> >                         {
> >                                 delete instance;
> >                                 instance = NULL;
> >                         }
> >                 }
> > };
> >
> > template<class IMPLEMENTINGCLASS> IMPLEMENTINGCLASS*
> > TSingleton<IMPLEMENTINGCLASS>::instance = NULL;
> 
> But I still have more then one instance:
> 
> before0
> my_singleton::my_singleton()
> after0x804a008
> before0
> my_singleton::my_singleton()
> after0x804a018
> 
what? 
show me your main please

regards
mws

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Singleton instantiated more than once with -fvisibility=hidden
  2005-06-03 14:09             ` Mws
@ 2005-06-03 14:20               ` Filipe Sousa
  0 siblings, 0 replies; 11+ messages in thread
From: Filipe Sousa @ 2005-06-03 14:20 UTC (permalink / raw)
  To: gcc-help


[-- Attachment #1.1: Type: text/plain, Size: 1553 bytes --]

On Friday 03 June 2005 15:09, Mws wrote:
> On Friday 03 June 2005 15:45, Filipe Sousa wrote:
> > On Friday 03 June 2005 14:22, Mws wrote:
> > > template<class IMPLEMENTINGCLASS>
> > > class TSingleton
> > > {
> > >         private:
> > >                 static IMPLEMENTINGCLASS* instance;
> > >
> > >         public:
> > >                 static IMPLEMENTINGCLASS* getInstance()
> > >                 {
> > >                         //debugging purposes
> > >                         std::cout << "before" << instance << std::endl;
> > >                         if(!instance)
> > >                         {
> > >                                 instance = new IMPLEMENTINGCLASS();
> > >                         }
> > >                         std::cout << "after" << instance << std::endl;
> > >                         return instance;
> > >                 }
> > >
> > >                 void dispose()
> > >                 {
> > >                         if(instance)
> > >                         {
> > >                                 delete instance;
> > >                                 instance = NULL;
> > >                         }
> > >                 }
> > > };
> > >
> > > template<class IMPLEMENTINGCLASS> IMPLEMENTINGCLASS*
> > > TSingleton<IMPLEMENTINGCLASS>::instance = NULL;
> >
> > But I still have more then one instance:
> >
> > before0
> > my_singleton::my_singleton()
> > after0x804a008
> > before0
> > my_singleton::my_singleton()
> > after0x804a018
>
> what?
> show me your main please
>
> regards
> mws

-- 
Filipe Sousa

[-- Attachment #1.2: main.cc --]
[-- Type: text/x-c++src, Size: 104 bytes --]

#include "singleton.h"

int main() {
    foo();
    my_singleton::getInstance()->foo();
    return 0;
}

[-- Attachment #1.3: singleton.h --]
[-- Type: text/x-c++hdr, Size: 932 bytes --]

#pragma once

#include <iostream>

#define EXPORT __attribute__ ((visibility("default")))

template<class IMPLEMENTINGCLASS>
class TSingleton
{
private:
    static IMPLEMENTINGCLASS* instance;

public:
    static IMPLEMENTINGCLASS* getInstance()
    {
        //debugging purposes
        std::cout << "before" << instance << std::endl;
        if(!instance)
        {
            instance = new IMPLEMENTINGCLASS();
        }
        std::cout << "after" << instance << std::endl;
        return instance;
    }

    void dispose()
    {
        if(instance)
        {
            delete instance;
            instance = NULL;
        }
    }
};

template<class IMPLEMENTINGCLASS>
IMPLEMENTINGCLASS* TSingleton<IMPLEMENTINGCLASS>::instance = NULL;

class EXPORT my_singleton : public TSingleton<my_singleton> {
public:
    my_singleton() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
    void foo() {}

};

EXPORT void foo();

[-- Attachment #1.4: singleton.cc --]
[-- Type: text/x-c++src, Size: 80 bytes --]

#include "singleton.h"

void foo()
{
    my_singleton::getInstance()->foo();
}


[-- Attachment #1.5: Makefile --]
[-- Type: text/x-makefile, Size: 372 bytes --]

all:
	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}

clean:
	rm -f *.o *.so main

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Singleton instantiated more than once with -fvisibility=hidden
  2005-06-03 14:52 Martin York
@ 2005-06-03 15:17 ` Filipe Sousa
  0 siblings, 0 replies; 11+ messages in thread
From: Filipe Sousa @ 2005-06-03 15:17 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1504 bytes --]

I'm aware of Singleton Pattern, my original code is a very simplistic 
implementation to show my problem. 

The point here is not about the quality of the pattern but the number of 
instances when using these flags: -fvisibility=hidden  
-fvisibility-inlines-hidden and __attribute__ ((visibility("default")))

Of course if I don't use these flags I only have one instance.

On Friday 03 June 2005 15:52, Martin York wrote:
> There are many ways to implement the Singleton Pattern. Each has their
> own positives and negatives. I would advice looking at "Scott Meyers"
> book "More Effective C++"
> http://www.amazon.co.uk/exec/obidos/ASIN/020163371X/ref=pd_bxgy_text_2_c
> p/026-5023597-8506032
>
>
> Personally I prefer being as simple as possible (Like your original
> code).
>
>
>
> The only problem with your original code is that
>
> singleton* singleton::instance()
>
> is defined in the header file. Therefore the implementation of instance
> is not in the resulting shared library.
>
> If you move the definition into the file singleton.cc then I believe
> (though not tested) that your problem will be resolved.

Can I move templated code to the implementation file? I thought that templated 
code should be written in the declaration file.

>
> As a secondary question/comment. Why return a pointer. To me this
> implies it can fail and return a NULL (and thus needs to be checked). I
> (personally) would return a reference.
[...]

-- 
Filipe Sousa

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: Singleton instantiated more than once with -fvisibility=hidden
@ 2005-06-03 14:52 Martin York
  2005-06-03 15:17 ` Filipe Sousa
  0 siblings, 1 reply; 11+ messages in thread
From: Martin York @ 2005-06-03 14:52 UTC (permalink / raw)
  To: Filipe Sousa, gcc-help


There are many ways to implement the Singleton Pattern. Each has their
own positives and negatives. I would advice looking at "Scott Meyers"
book "More Effective C++"
http://www.amazon.co.uk/exec/obidos/ASIN/020163371X/ref=pd_bxgy_text_2_c
p/026-5023597-8506032


Personally I prefer being as simple as possible (Like your original
code).



The only problem with your original code is that 

singleton* singleton::instance()

is defined in the header file. Therefore the implementation of instance
is not in the resulting shared library.

If you move the definition into the file singleton.cc then I believe
(though not tested) that your problem will be resolved.


As a secondary question/comment. Why return a pointer. To me this
implies it can fail and return a NULL (and thus needs to be checked). I
(personally) would return a reference.


singleton.cc

singleton&  singleton::instance()
{
    static singleton   myInstance;
    return(myInstance);
}

There is a small caveat to implementing the singleton this way. The
problem of the singleton being used by another singleton (that is
created before this one) in its destructor. [I'll let you think about
why that is a problem] Solutions to this and other problem are discussed
in "Scott Meyers" book.

PS. Because C++ automatically defines certain methods and to make sure
no other copies of a singleton can be created the following methods
should be defined as private:

class singleton
{
    public:
        singleton& instance();
    private:
         singleton();    // provide definition
        ~singleton();    // provide definition

         /*
          * The following methods should be declared so that the
compiler
          * does not generate default implementations of them.
          * But as they will never be used you do not need to actually
define them.
          */
         singleton(const singleton& copy);            // No definition
required.
         singleton& operator=(const singleton& copy); // No definition
required.
};


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2005-06-03 15:17 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-06-03 10:25 Singleton instantiated more than once with -fvisibility=hidden Filipe Sousa
2005-06-03 10:29 ` Mws
2005-06-03 12:40   ` Filipe Sousa
2005-06-03 12:46     ` Mws
2005-06-03 13:09       ` Filipe Sousa
2005-06-03 13:22         ` Mws
2005-06-03 13:45           ` Filipe Sousa
2005-06-03 14:09             ` Mws
2005-06-03 14:20               ` Filipe Sousa
2005-06-03 14:52 Martin York
2005-06-03 15:17 ` Filipe Sousa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).