Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. is it possible to call class in destructor ? if not then why ?
QtWS25 Last Chance

is it possible to call class in destructor ? if not then why ?

Scheduled Pinned Locked Moved Solved C++ Gurus
4 Posts 2 Posters 347 Views
  • 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.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on last edited by
    #1

    My code is shown below :

    WaybillCloseFlow::~WaybillCloseFlow() {
    	// TODO Auto-generated destructor stub
    	WaybillCollectionReportFlow *pWaybillCollectionReportFlow = new WaybillCollectionReportFlow;
    	pWaybillCollectionReportFlow->execute();
    }
    
    JonBJ 1 Reply Last reply
    0
    • Q Qt embedded developer

      My code is shown below :

      WaybillCloseFlow::~WaybillCloseFlow() {
      	// TODO Auto-generated destructor stub
      	WaybillCollectionReportFlow *pWaybillCollectionReportFlow = new WaybillCollectionReportFlow;
      	pWaybillCollectionReportFlow->execute();
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Qt-embedded-developer
      And what is your issue? You create a new instance of an unrelated class and call a method on it. (And then fail to delete it, so it should leak.) May or may not be a good thing to do, but certainly allowable.

      Q 1 Reply Last reply
      1
      • JonBJ JonB

        @Qt-embedded-developer
        And what is your issue? You create a new instance of an unrelated class and call a method on it. (And then fail to delete it, so it should leak.) May or may not be a good thing to do, but certainly allowable.

        Q Offline
        Q Offline
        Qt embedded developer
        wrote on last edited by Qt embedded developer
        #3

        @JonB now i am adding below part of code

        WaybillCloseFlow::~WaybillCloseFlow() {
           // TODO Auto-generated destructor stub
           WaybillCollectionReportFlow *pWaybillCollectionReportFlow = new WaybillCollectionReportFlow;
           pWaybillCollectionReportFlow->execute();
        
        if(pWaybillCollectionReportFlow)
        {
        delete pWaybillCollectionReportFlow;
        pWaybillCollectionReportFlow = NULL;
        }
        
        }
        

        Then is it possible without memory leak or not ?

        JonBJ 1 Reply Last reply
        0
        • Q Qt embedded developer

          @JonB now i am adding below part of code

          WaybillCloseFlow::~WaybillCloseFlow() {
             // TODO Auto-generated destructor stub
             WaybillCollectionReportFlow *pWaybillCollectionReportFlow = new WaybillCollectionReportFlow;
             pWaybillCollectionReportFlow->execute();
          
          if(pWaybillCollectionReportFlow)
          {
          delete pWaybillCollectionReportFlow;
          pWaybillCollectionReportFlow = NULL;
          }
          
          }
          

          Then is it possible without memory leak or not ?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Qt-embedded-developer
          Yes this will of course prevent the leak. However there's no need to do this if you allocate on stack instead of heap:

             WaybillCollectionReportFlow waybillCollectionReportFlow;
             waybillCollectionReportFlow.execute();
          

          That's it, no new or delete or leak.

          1 Reply Last reply
          3

          • Login

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