0

Hi All,
I know this is very silly question about singleton pattern but still it is first choice of interviewer.
Could you let me know in below code snippet.

(1) After deleting singleton object why still i am able to call show() method and it works fine.

delete obj;
obj=NULL;
obj->show();

(2) After creating obj1 object why i am not able to print the content of acquire_lock and release_lock function even print statment "one Time" will be printed once and if we increment counter i then instead of 2 it is printing 1 only, why?

Foo *obj1=MySingleton<Foo>::GetInstance();

code snippet:

#include <iostream>
#include <fstream>
#include <memory>
#include <string>
using namespace std;
static int i;
class Lock
{
public:
Lock(){};
~Lock(){};
void acquire_lock()
{
cout<<"aquired lock for class";
}

void release_lock()
{
cout<<"released lock for class";
}

};

class Foo
{
public:
void show()
{
cout<<"\ndone\n"; 
}

};

template <class T>

class MySingleton
{
       protected:

      MySingleton()
      {

      }

      private:
      //holds one and only object of MySingleton
      MySingleton(const MySingleton <T> &) {};
      MySingleton <T> & operator=(const MySingleton <T> &) {};
      static T* m_pOnlyOneInstance;
      ~MySingleton() {};

     public:
      // MySingleton functionalities
     static T * GetInstance();

      void foo()
      {
      cout<<"Mohan";
      }

};

//with thread approach

template <class T>
 T*  MySingleton<T>::GetInstance()
{             Lock lock;

            if (m_pOnlyOneInstance == NULL)
        {  
             lock.acquire_lock();
           cout<<"one Time"<<endl;  
               i++; 
        if(m_pOnlyOneInstance == NULL)
                {

                m_pOnlyOneInstance = new T();


                }
        lock.release_lock();

         }  




      return m_pOnlyOneInstance;
}

template <class T> T* MySingleton<T> :: m_pOnlyOneInstance=NULL;
int main()
{

//std::unique_ptr <Foo> obj (MySingleton<Foo>::GetInstance());
Foo *obj=MySingleton<Foo>::GetInstance();    
//obj->show();
delete obj;
obj=NULL;
obj->show();
cout<<"\ncalling again\n";
Foo *obj1=MySingleton<Foo>::GetInstance();
obj1->show();
cout<<"i="<<i;  
return 1;   
}

Note: lock related function are dummy implementationonly

2
Contributors
3
Replies
23
Views
2 Years
Discussion Span
Last Post by Moschops
0

I haven't double-checked what I'm writing here, but it seems very familiar.

In C++ implementations, class functions are shared. Every instance of a class uses the same functions. When you call the function, you're not going through any particular instance. You're calling the function, and the function is given a pointer to the actual instance of the class you're calling it on. It's the function that knows about the instance in C++.

obj->show(); is calling the show function. The show function will have some kind of bad pointer to the actual instance, because you set that pointer to NULL, but because the show() function doesn't actually need anything from that instance, it doesn't use the bad pointer and you don't get a segFault.

If you add a member variable to the class, and then change your show() function so it's actually using that member variable, you'll see it segFault.

Edited by Moschops

0

if we don't set that pointer to NULL still it works .

That's right. obj->show() calls the show() function (without using the pointer), and the show() function makes use of the pointer, so if the pointer is in a bad state, it will only be discovered somewhere inside the show() function.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.