0

I'm working with the qpid messaging library. I have a singleton class that holds connection objects. I noticed that my application would core on exit depending on how I created the connection. If I created it in the singleton constructor or a static method everything was fine, however if I created it in a non-static method, or in my getinstance method as shown below the messaging library would internally call a close method before my destructor was called and it would call an abort. I was wondering what about these creation calls is different that could cause such a result.

class Singleton : public boost::noncopyable
{
public:

  static Singleton &
  GetInstance()
  {
    static Singleton tS;
    // If I create _Conn this way it cores
    // tS._Conn = SharedConn(new Conn());
    return tS;
  }

private:

  typedef boost::shared_ptr< Conn > SharedConn;
  Singleton()
  {
    // If I create _Conn this way it works
    //_Conn = SharedConn(new Conn());
  }

  SharedConn _Conn;
};

int main(int argc, char** argv)
{
  Singleton::GetInstance();
  return 0;
}
sfpiano
  • 369
  • 1
  • 8
  • 1
    Hi, what happens if GetInstance() is called twice in a row? It seems that in the first case (case which works), everything's all right, tS.Conn is not called twice as the constructor is not called again; however in the second case (case which cores), at the second call to getInstance(), tS._Conn is reassigned and the prevous connection is lost... Correct me if I'm wrong – GL770 Jul 11 '12 at 19:49
  • I only instantiate _Conn one time. If I instantiate it in the constructor it works. If I instantiate it in the getinstnace call it cores. – sfpiano Jul 11 '12 at 20:02

1 Answers1

0

Is there a reason why you re-create _Conn in GetInstance. At that point constructor has already created it once. So, re-assigning to _Conn is likely to cause call of destructor (and close) of the previous instance.

Jari
  • 2,152
  • 13
  • 20
  • Sorry, I don't actually call both of those methods together. When it doesn't crash I only instantiate it in the constructor and when it does crash I only instantiate it in the getinstance method. – sfpiano Jul 11 '12 at 19:30
  • So, in case you use getinstance your constructor is empty? – Jari Jul 11 '12 at 19:36