Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. GoogleTest: Mock object should be deleted but never is.
Forum Updated to NodeBB v4.3 + New Features

GoogleTest: Mock object should be deleted but never is.

Scheduled Pinned Locked Moved Unsolved 3rd Party Software
11 Posts 3 Posters 9.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    Why are you creating that object on the heap rather than the stack ?

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    H 1 Reply Last reply
    1
    • SGaistS SGaist

      Hi,

      Why are you creating that object on the heap rather than the stack ?

      H Offline
      H Offline
      henrik2016
      wrote on last edited by
      #3

      @SGaist I have edited my post.

      1 Reply Last reply
      0
      • H henrik2016

        Hi,
        it's the first time to work with GoogleTest. I created a few Mock-Objects, but I don't know how I can delete it.

        Error message for each (19) Mock-Object in opcNodeList is: Mock object should be deleted but never is.
        Last error message: ERROR: 19 leaked mock objects found at program exit.

        Any ideas?

        class TestOpcUa : public Test
        {
        public:
            TestOpcUa() 
            {
                _selectedConfValid = new MockOpcConf();
                ON_CALL(*_selectedConfValid, getSERVERNAME()).WillByDefault(Return(END_POINT_VALID));
                ON_CALL(*_selectedConfValid, getOpcNodeList()).
                    WillByDefault(Invoke( [] ()
                    {
                        OpcNodeList* opcNodeList = new OpcNodeList();
                        shared_ptr<MockOpcNode> deviceNo = make_shared<MockOpcNode>();
                        ON_CALL(*deviceNo, getDISPLAY_NAME()).WillByDefault(Return("DeviceNo"));
                        ON_CALL(*deviceNo, getDATATYPEID()).WillByDefault(Return(4));
                        ON_CALL(*deviceNo, getPARENTNODE()).WillByDefault(Return("ns=2;s=TestChannel.SPS.DB115"));
                        ON_CALL(*deviceNo, getCHILDNODE()).WillByDefault(Return("ns=2;s=DBW0"));
                        opcNodeList->push_back(deviceNo);
                        //...push_back more Mock-Objects
                        return opcNodeList;
                    }));
                _opcUaObjectValid = new OpcUa(_selectedConfValid);
            }
            ~TestOpcUa() 
            {
                delete _selectedConfValid;
                delete _opcUaObjectValid;
            }
        protected:
                MockOpcConf* _selectedConfValid = nullptr;
                OpcUa* _opcUaObjectValid = nullptr;
        }
        
        TEST_F(TestOpcUa, firstTest)
        {
            //......Tests are valid
        }
        
        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @henrik2016 said in GoogleTest: Mock object should be deleted but never is.:

        _selectedConfValid = new MockOpcConf();

        Youre still creating it on the heap, why not simply:

        TestOpcUa() 
            {
                MockOpcConf _selectedConfValid; // Allocate on the stack, no need to delete explicetly
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        H 1 Reply Last reply
        0
        • jsulmJ jsulm

          @henrik2016 said in GoogleTest: Mock object should be deleted but never is.:

          _selectedConfValid = new MockOpcConf();

          Youre still creating it on the heap, why not simply:

          TestOpcUa() 
              {
                  MockOpcConf _selectedConfValid; // Allocate on the stack, no need to delete explicetly
          
          H Offline
          H Offline
          henrik2016
          wrote on last edited by
          #5

          @jsulm The function I would like to test need pointer.
          I have got a Destructor in OpcConf. Is it possible to add a destructor in the Mock-Class MockOpcConf?

          jsulmJ 1 Reply Last reply
          0
          • H henrik2016

            @jsulm The function I would like to test need pointer.
            I have got a Destructor in OpcConf. Is it possible to add a destructor in the Mock-Class MockOpcConf?

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #6

            @henrik2016 said in GoogleTest: Mock object should be deleted but never is.:

            The function I would like to test need pointer.

            And what is the problem? Just pass it the pointer then:

            &_selectedConfValid
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • H Offline
              H Offline
              henrik2016
              wrote on last edited by
              #7

              @jsulm But how can I modify the lambda function?

              jsulmJ 1 Reply Last reply
              0
              • H henrik2016

                @jsulm But how can I modify the lambda function?

                jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @henrik2016 What exactly do you want to change?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  henrik2016
                  wrote on last edited by
                  #9

                  @jsulm I create 19 Mock-Objects like this:

                  shared_ptr<MockOpcNode> deviceNo = make_shared<MockOpcNode>();

                  At the end of my test, I have to delete all objects. How can I do this?

                  jsulmJ 1 Reply Last reply
                  0
                  • H henrik2016

                    @jsulm I create 19 Mock-Objects like this:

                    shared_ptr<MockOpcNode> deviceNo = make_shared<MockOpcNode>();

                    At the end of my test, I have to delete all objects. How can I do this?

                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    @henrik2016 said in GoogleTest: Mock object should be deleted but never is.:

                    At the end of my test, I have to delete all objects. How can I do this?

                    You don't if you do not allocate on the heap (don't use new) - that was the whole point of what @SGaist wrote.
                    So, why do you do it this way

                    _selectedConfValid = new MockOpcConf();
                    

                    intead of

                    MockOpcConf _selectedConfValid;
                    

                    ?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      henrik2016
                      wrote on last edited by henrik2016
                      #11

                      @jsulm I already use MockOpcConf _selectedConfValid;

                      But I have got the other objects.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved