Singleton question.

Discussion in 'C++' started by prawit.c@gmail.com, May 31, 2005.

  1. Guest

    Hi, all
    I've written a program to produce singleton object.
    Like this.

    ---------------------------------------------------
    class A{
    public:
    static A* getInstance();
    void print(); \\ print out 'a' I won't implement here.
    void setA(int); \\ set 'a', I won't implement here.
    ~A(){}
    protected:
    A(){}
    private:
    int a;
    static A* instance;
    };

    A* A::instance = NULL;

    A* A::getInstance()
    {
    if(instance)
    return instance;
    else
    return (instance=new A());
    }

    int main()
    {
    A * a = A::getInstance();
    a->setA(10);
    a->print();
    delete a; // <-- This line is a question. Read below.

    a->setA(15); // <-- This line should crash, isn't it?
    a->print(); // <-- This line should crash, isn't it?

    return 0;
    }
    -----------------------------------------------------------
    The result of this program is:
    #./SingleTon
    10
    15
    #
    ------------------------------------------------------------
    My question is:
    "Why instance of class A still exist? "
    My understanding is we've deleted that object (line 'delete a;')


    Thank in advance.
    Prawit Chaivong.
    , May 31, 2005
    #1
    1. Advertising

  2. Srini Guest

    > delete a; // <-- This line is a question. Read below.
    >
    > a->setA(15); // <-- This line should crash, isn't it?
    > a->print(); // <-- This line should crash, isn't it?
    >
    > return 0;
    > }
    > -----------------------------------------------------------
    > The result of this program is:
    > #./SingleTon
    > 10
    > 15
    > #
    > ------------------------------------------------------------
    > My question is:
    > "Why instance of class A still exist? "
    > My understanding is we've deleted that object (line 'delete a;')
    >


    This is undefined behavior. One cannot expect anything definitive when
    you access members after deleting an object. Its a different matter
    that you _should not_ do that!

    Regards,
    Srini



    wrote:
    > Hi, all
    > I've written a program to produce singleton object.
    > Like this.
    >
    > ---------------------------------------------------
    > class A{
    > public:
    > static A* getInstance();
    > void print(); \\ print out 'a' I won't implement here.
    > void setA(int); \\ set 'a', I won't implement here.
    > ~A(){}
    > protected:
    > A(){}
    > private:
    > int a;
    > static A* instance;
    > };
    >
    > A* A::instance = NULL;
    >
    > A* A::getInstance()
    > {
    > if(instance)
    > return instance;
    > else
    > return (instance=new A());
    > }
    >
    > int main()
    > {
    > A * a = A::getInstance();
    > a->setA(10);
    > a->print();
    > delete a; // <-- This line is a question. Read below.
    >
    > a->setA(15); // <-- This line should crash, isn't it?
    > a->print(); // <-- This line should crash, isn't it?
    >
    > return 0;
    > }
    > -----------------------------------------------------------
    > The result of this program is:
    > #./SingleTon
    > 10
    > 15
    > #
    > ------------------------------------------------------------
    > My question is:
    > "Why instance of class A still exist? "
    > My understanding is we've deleted that object (line 'delete a;')
    >
    >
    > Thank in advance.
    > Prawit Chaivong.
    Srini, May 31, 2005
    #2
    1. Advertising

  3. Guest

    What do you think if I prevent this object from deleting.
    By declare Dtor as protected.
    , May 31, 2005
    #3
  4. Srini Guest

    > What do you think if I prevent this object from deleting.

    You can return a static reference instead of a pointer to the lone
    object.

    static A& A::getInstance(void)
    {
    static A instance;
    return instance;
    }

    But this method will have implications in a multi-threaded application.


    > By declare Dtor as protected.


    Destructors must NOT be made private! The compiler would not allow you
    to do that.

    Regards,
    Srini
    Srini, May 31, 2005
    #4
  5. Old Wolf Guest

    Srini wrote:
    >
    > > By declare Dtor as protected.

    >
    > Destructors must NOT be made private! The compiler would not allow you
    > to do that.


    Destructors can be protected or private, and the compiler
    would allow it. It would be useful in exactly this case,
    to prevent the main program from deleting the object.
    Old Wolf, May 31, 2005
    #5
  6. Srini Guest

    > Destructors can be protected or private, and the compiler
    > would allow it. It would be useful in exactly this case,
    > to prevent the main program from deleting the object.


    I stand corrected. Only if there's an object instance that needs to be
    destroyed and the compiler finds the destructor is private, does it
    throw up an error. Thanks Old Wolf!

    Srini
    Srini, May 31, 2005
    #6
  7. Usenet Guest

    "Srini" <> wrote in message
    news:...
    > > What do you think if I prevent this object from deleting.

    >
    > You can return a static reference instead of a pointer to the lone
    > object.
    >
    > static A& A::getInstance(void)
    > {
    > static A instance;
    > return instance;
    > }
    >
    > But this method will have implications in a multi-threaded application.


    Can you tell about some implications plz?

    >
    >
    > > By declare Dtor as protected.

    >
    > Destructors must NOT be made private! The compiler would not allow you
    > to do that.
    >
    > Regards,
    > Srini
    >
    Usenet, May 31, 2005
    #7
  8. Srini Guest

    > > But this method will have implications in a multi-threaded application.

    > Can you tell about some implications plz?


    The version of the singleton implementation that returns a static
    pointer, would use operator new to allocate the lone object. Because
    operator new is thread-safe it can be safely used in a multithreaded
    app. Moreover with the static pointer version, its our responsibility
    to delete the lone object. But with the second version, it gets
    destructed automatically when the app terminates.
    Srini, May 31, 2005
    #8
  9. Guest

    I don't think a caller has responsibility to delete that object since
    it might be used in somewhere else.
    , May 31, 2005
    #9
  10. Srini Guest

    > I don't think a caller has responsibility to delete that object since
    > it might be used in somewhere else.


    I did not mean the 'caller'. Whoever writes such a class must provide
    some means for cleaning up.

    Srini
    Srini, Jun 1, 2005
    #10
  11. Laurens Guest

    You could assign the instance to a global or module-level auto_ptr (or
    boost::scoped_ptr) to have it cleaned up automatically upon program
    termination.

    Regards
    -Laurens
    Laurens, Jun 1, 2005
    #11
    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,222
    Proton Projects - Moin
    Mar 27, 2007
  2. Wilhelm
    Replies:
    1
    Views:
    158
  3. Trans
    Replies:
    12
    Views:
    269
    Robert Klemme
    Sep 14, 2007
  4. Paul McMahon
    Replies:
    3
    Views:
    198
    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:
    198
    Charles Oliver Nutter
    Mar 22, 2010
Loading...

Share This Page