singleton question

Discussion in 'C++' started by tarmat, Oct 31, 2003.

  1. tarmat

    tarmat Guest

    I have a singleton class that looks a little like this:

    class MyClass
    {
    private:

    //data

    MyClass()
    {
    Create();
    }

    void Create(); //initialization stuff

    public:

    static MyClass* Instance()
    {
    static MyClass instance;

    return &instance;
    }

    //interface
    };

    This singleton class is used within multiple cpp files within my
    project. It works fine in debug build but when I compile a release
    build the MyClass ctor is called multiple times, one time for each
    different cpp file it is called from.

    Do you know why this is happening? I don't understand what's going on
    at all. Surely it is only possible for the ctor to be called once
    given this code.

    Thanks for any enlightenment.
     
    tarmat, Oct 31, 2003
    #1
    1. Advertising

  2. tarmat

    Ralf Guest

    Hi,

    if you are defining your static Instance() method inside a header file, this
    should result in an behaviour as you described. Because you have more than
    only one static instance of your singleton class.
    If you are defining the Instance() method outside of the class in a cpp
    file, it should work correctly.

    Ralf



    www.cplusplus-kurse.de


    "tarmat" <> schrieb im Newsbeitrag
    news:...
    > I have a singleton class that looks a little like this:
    >
    > class MyClass
    > {
    > private:
    >
    > //data
    >
    > MyClass()
    > {
    > Create();
    > }
    >
    > void Create(); //initialization stuff
    >
    > public:
    >
    > static MyClass* Instance()
    > {
    > static MyClass instance;
    >
    > return &instance;
    > }
    >
    > //interface
    > };
    >
    > This singleton class is used within multiple cpp files within my
    > project. It works fine in debug build but when I compile a release
    > build the MyClass ctor is called multiple times, one time for each
    > different cpp file it is called from.
    >
    > Do you know why this is happening? I don't understand what's going on
    > at all. Surely it is only possible for the ctor to be called once
    > given this code.
    >
    > Thanks for any enlightenment.
     
    Ralf, Oct 31, 2003
    #2
    1. Advertising

  3. tarmat

    Tim Clacy Guest

    tarmat wrote:
    > I have a singleton class that looks a little like this:
    >
    > class MyClass
    > {
    > private:
    >
    > //data
    >
    > MyClass()
    > {
    > Create();
    > }
    >
    > void Create(); //initialization stuff
    >
    > public:
    >
    > static MyClass* Instance()
    > {
    > static MyClass instance;
    >
    > return &instance;
    > }
    >
    > //interface
    > };
    >
    > This singleton class is used within multiple cpp files within my
    > project. It works fine in debug build but when I compile a release
    > build the MyClass ctor is called multiple times, one time for each
    > different cpp file it is called from.
    >
    > Do you know why this is happening? I don't understand what's going on
    > at all. Surely it is only possible for the ctor to be called once
    > given this code.
    >
    > Thanks for any enlightenment.


    If you move the definition of MyClass::Instance() into a '.cpp' file, and
    that works, then the problem is that your compiler doesn't make static data
    defined in inline member functions refer to the same item; I only know this
    through similar experience with two different compilers. On older compilers,
    if static class data is defined in headers, then every module that includes
    that header gets its own unique static data; newer compilers ensure that
    there is only one instance if the data.

    Tim
     
    Tim Clacy, Oct 31, 2003
    #3
  4. tarmat

    tarmat Guest

    thanks guys, I didn't know that
     
    tarmat, Oct 31, 2003
    #4
  5. tarmat

    stephan beal Guest

    tarmat wrote:
    > static MyClass* Instance()
    > {
    > static MyClass instance;
    >
    > return &instance;
    > }


    IMO a pointer is the wrong thing to return there. Passing a non-const
    pointer often implies to the client that the caller owns the returned
    pointer. Passing a reference gives the clear message that the object is not
    to be deleted by clients.

    > This singleton class is used within multiple cpp files within my
    > project. It works fine in debug build but when I compile a release
    > build the MyClass ctor is called multiple times, one time for each
    > different cpp file it is called from.


    Remove the 'static' part if you compile this inline. (i learned this lesson
    only a few weeks ago, in a case almost identical to yours.)


    --
    ----- stephan beal
    http://s11.net/
    Registered Linux User #71917 http://counter.li.org
    I speak for myself, not my employer. Contents may
    be hot. Slippery when wet. Reading disclaimers makes
    you go blind. Writing them is worse. You have been Warned.
     
    stephan beal, Oct 31, 2003
    #5
  6. tarmat

    Tim Clacy Guest

    stephan beal wrote:
    > tarmat wrote:
    >> static MyClass* Instance()
    >> {
    >> static MyClass instance;
    >>
    >> return &instance;
    >> }

    >
    > IMO a pointer is the wrong thing to return there. Passing a non-const
    > pointer often implies to the client that the caller owns the returned
    > pointer. Passing a reference gives the clear message that the object
    > is not to be deleted by clients.
    >
    >> This singleton class is used within multiple cpp files within my
    >> project. It works fine in debug build but when I compile a release
    >> build the MyClass ctor is called multiple times, one time for each
    >> different cpp file it is called from.

    >
    > Remove the 'static' part if you compile this inline. (i learned this
    > lesson only a few weeks ago, in a case almost identical to yours.)


    Stephan,

    Hi. The poster can't simply remove 'static'; to do so would mean that
    instance is a temporary object and Instance() would be returning a pointer
    to that temporary object. Also the singleton would be constructed and
    destructed every time someone tried to get a handle to it... which is
    probably not what's wanted.

    The options are:

    1) move MyClass::Instance() to a '.cpp' file
    2) move 'instance' out of Instance() and into MyClass (keeping it static) an
    define it in a '.cpp' file
    3) find one of these trendy compilers that knows what to do

    Personally, I like 3) since it gets rid of the need for '.cpp' files that
    contain one function :)


    Tim
     
    Tim Clacy, Oct 31, 2003
    #6
  7. tarmat

    jeffc Guest

    "tarmat" <> wrote in message
    news:...
    > I have a singleton class that looks a little like this:
    >
    > class MyClass
    > {
    > private:
    >
    > //data
    >
    > MyClass()
    > {
    > Create();
    > }
    >
    > void Create(); //initialization stuff
    >
    > public:
    >
    > static MyClass* Instance()
    > {
    > static MyClass instance;
    >
    > return &instance;
    > }
    >
    > //interface
    > };
    >
    > This singleton class is used within multiple cpp files within my
    > project. It works fine in debug build but when I compile a release
    > build the MyClass ctor is called multiple times, one time for each
    > different cpp file it is called from.
    >
    > Do you know why this is happening? I don't understand what's going on
    > at all. Surely it is only possible for the ctor to be called once
    > given this code.


    No, I don't think this is a standard approach. You're creating a static
    instance in your *header* file. You want one instance per application, not
    one per inclusion of header file. (By the way, just because you have a
    singleton class doesn't necessarily mean you *have* to have an instance.
    Are you sure you don't want to create that instance somewhere else, if and
    when you need it?)
     
    jeffc, Oct 31, 2003
    #7
  8. tarmat

    stephan beal Guest

    Tim Clacy wrote:
    > Hi. The poster can't simply remove 'static'; to do so would mean that
    > instance is a temporary object and Instance() would be returning a pointer
    > to that temporary object. Also the singleton would be constructed and


    Sorry, you misunderstood (and i was ambiguous): i meant the static qualifier
    from the function, not the internal static variable.

    --
    ----- stephan beal
    http://s11n.net/
    Registered Linux User #71917 http://counter.li.org
    I speak for myself, not my employer. Contents may
    be hot. Slippery when wet. Reading disclaimers makes
    you go blind. Writing them is worse. You have been Warned.
     
    stephan beal, Oct 31, 2003
    #8
  9. tarmat

    Tim Clacy Guest

    stephan beal wrote:
    > Tim Clacy wrote:
    >> Hi. The poster can't simply remove 'static'; to do so would mean that
    >> instance is a temporary object and Instance() would be returning a
    >> pointer to that temporary object. Also the singleton would be
    >> constructed and

    >
    > Sorry, you misunderstood (and i was ambiguous): i meant the static
    > qualifier from the function, not the internal static variable.


    Stephan,

    ....but if you remove the static qualifier from 'Instance()', you will need
    an instance of the class to get to the the 'Instance()' member function;
    it's not a singleton anymore.

    Tim
     
    Tim Clacy, Nov 3, 2003
    #9
  10. tarmat

    stephan beal Guest

    Tim Clacy wrote:
    > Stephan,
    >
    > ...but if you remove the static qualifier from 'Instance()', you will need
    > an instance of the class to get to the the 'Instance()' member function;
    > it's not a singleton anymore.


    Oh, doh.
    /slap forehead.

    --
    ----- stephan beal
    http://s11n.net/
    Registered Linux User #71917 http://counter.li.org
    I speak for myself, not my employer. Contents may
    be hot. Slippery when wet. Reading disclaimers makes
    you go blind. Writing them is worse. You have been Warned.
     
     
    stephan beal, Nov 3, 2003
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,259
    Proton Projects - Moin
    Mar 27, 2007
  2. Wilhelm
    Replies:
    1
    Views:
    167
  3. Trans
    Replies:
    12
    Views:
    280
    Robert Klemme
    Sep 14, 2007
  4. Paul McMahon
    Replies:
    3
    Views:
    207
    David A. Black
    Jun 9, 2008
  5. Charles Oliver Nutter

    Singleton methods without the singleton class

    Charles Oliver Nutter, Mar 15, 2010, in forum: Ruby
    Replies:
    4
    Views:
    204
    Charles Oliver Nutter
    Mar 22, 2010
Loading...

Share This Page