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. [solved] how to delete pointers before main() exits ?
QtWS25 Last Chance

[solved] how to delete pointers before main() exits ?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.5k 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.
  • T3STYT Offline
    T3STYT Offline
    T3STY
    wrote on last edited by
    #1

    Consider the next code:

    @int main(int argc, char *argv[]){
    QApplication app(argc, argv);

    QWidget * mainWindow = new QWidget;
    mainWindow->show();

    MyWidget * myW = new MyWidget;
    myW->show();

    // some other code

    // delete mainWindow; //if I use delete, they destroy the widgets before I can use them
    // delete myW; // if not, a memory leak occurs... what should I do?
    return app.exec();
    }@

    When I run it my application runs just fine. But for the concept of allocating objects on the heap, I should manually delete the _mainWindow _and _myW _pointers.
    Problem is that if I call delete on these pointers anywhere in main() (f.e. before return), the object destructors are called too and my widgets get destroyed little time after creation without even being able to use them for a second. But if I don't call delete, I am creating a memory leak since the heap memory allocated for _mainWindow _and _myW _is not deleted.
    At this point I'm really stuck on how should I delete these pointers, I can't seem to find any good place to delete the pointers without destroying the widgets too... any suggestions ?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      @
      int ret = app.exec();
      delete mainWindow;
      delete myW;

      return ret;
      @

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Stian Andre Olsen
        wrote on last edited by
        #3

        Two other options:

        1. Allocate your main window on the stack, e.g.:
          @QWidget mainWindow; // using a QWidget
          QMainWindow mainWindow; // or maybe a QMainWindow@

        2. Allocate your main window on the heap and call
          @mainWindow->setAttribute(Qt::WA_DeleteOnClose);@

        When it comes to your other widget, you can choose the same approach but if the widget is a child window to your main window a better approach is to create it using your mainWindow as a parent, e.g.:

        @// if mainWidget is allocated on the stack
        MyWidget *myW = new MyWidget(&mainWindow);

        // if mainWidget is allocated on the heap
        MyWidget *myW = new MyWidget(mainWindow);@

        Then your widget will be deleted just before your mainWindow (when your mainWindow deletes its children).

        Stian Andre Olsen

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dbzhang800
          wrote on last edited by
          #4

          smart pointer can be used in such cases too, such as

          @
          QScopedPointer<QWidget> mainWindow(new QWidget);
          mainWindow->show();

          QScopedPointer<MyWidget> myW(new MyWidget);
          myW->show();
          @

          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