Menu

Testing a singleton class

Help
ritesh
2003-01-28
2003-01-29
  • ritesh

    ritesh - 2003-01-28

    Hi
         I am implementing CppUnit-1.8.0 on one of the project.
    In his project our team is using singleton class.
    I am facing some problem with setUp() and tearDown() for the GUI output.
    if i run the suite second time seUp is failing or some time i am getting any error. because in tear down deletion is not proper..

    setUp and tearDown code is as follows :

    void CLPvalidationChildTest::setUp()
    {
        op1=new CCLPoption();
        op2=new CCLPoption();
        setOption();
        l_cclholder= CCLPinputHolder::getInstance();
        l_cclholder->add(*op1);
        cv=new CCLPvalidationChild();
       
    }

    void CLPvalidationChildTest::tearDown()
    {
        delete op1;
        delete op2;
        delete cv;
        l_cclholder->deleteMap();
        delete l_cclholder;
    }

    If i comment out delete l_cclholder;
    then i am getting some error.
    if not then on running the same suite on the GUI. then setUp failed is coming up.

    please tell me how to create the object of the singleton class and delete it.

    Regards

    - Ritesh

     
    • Tom Plunket

      Tom Plunket - 2003-01-28

      You don't delete singletons like that.  Read Alexandrescu's "Modern C++ Design" to learn about them.

      setUp() is failing because you're deleting something that you don't own.  That you're getting "some error" is definitely a problem, but not enough info to work with.

       
    • Johan Nilsson

      Johan Nilsson - 2003-01-29

      Basic steps:

      0) Consider whether a singleton is really necessary
      .... consider it once more ...
      .... ok, you need a singleton - try this :

      1) Make the class a non-singleton.
      2) Test away
      3) Make a singleton implementation of the class, e.g.:

      struct GlobalCCLPinputHolder
      {
      private:
          GlobalCCLPinputHolder(){}
          GlobalCCLPinputHolder(const GlobalCCLPinputHolder&);
          GlobalCCLPinputHolder& operator=(const GlobalCCLPinputHolder&);

      public:
          static CCLPinputHolder& getInstance()
          { static CCLPinputHolder theInstance; return theInstance; }
      };

      4) Use the singleton implementation in production

      HTH // Johan

       
      • Johan Nilsson

        Johan Nilsson - 2003-01-29

        Correction (this looks better, at least to me):

        -------
        struct GlobalCCLPinputHolder : public CCLPinputHolder
        {
        private:
             GlobalCCLPinputHolder(){}
             GlobalCCLPinputHolder(const GlobalCCLPinputHolder&);
             GlobalCCLPinputHolder& operator=(const GlobalCCLPinputHolder&);

        public:
             static GlobalCCLPinputHolder& getInstance()
             { static GlobalCCLPinputHolder theInstance; return theInstance; }
        };
        -----

        // Johan

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.