Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Where to call destructor for new in main()
Qt 6.11 is out! See what's new in the release blog

Where to call destructor for new in main()

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.1k 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.
  • gde23G Offline
    gde23G Offline
    gde23
    wrote on last edited by
    #1

    Hello,

    i have kind of a noob question.

    if a allocate an object (SomeObject) in the main() function. Where do i call the destructor on application closing.

    int main(int argc, char *argv[]){
        QApplication a(argc, argv);
        MainWindow w;
        SomeObject* so =  new SomeObject();
    
        w.show();    
        return a.exec();
    }
    
    mrjjM 1 Reply Last reply
    0
    • gde23G gde23

      Hello,

      i have kind of a noob question.

      if a allocate an object (SomeObject) in the main() function. Where do i call the destructor on application closing.

      int main(int argc, char *argv[]){
          QApplication a(argc, argv);
          MainWindow w;
          SomeObject* so =  new SomeObject();
      
          w.show();    
          return a.exec();
      }
      
      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      @gde23
      Hi
      u can split return a.exec();
      so its

      auto code = a.exec();
      delete xxxxx
      return code;

      But you can also do
      SomeObject so; // no need for new

      as main is not running out of scope until exec() exits.

      Also say it was QButton and u new one and give it to say a Dialog, then dialog will delete it. All widgets are deleted by their owner/parent.
      You own class if not QObject will ofc. not.

      1 Reply Last reply
      2
      • gde23G Offline
        gde23G Offline
        gde23
        wrote on last edited by
        #3

        @mrjj Thanks

        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