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. DeleteLater never deletes...
QtWS25 Last Chance

DeleteLater never deletes...

Scheduled Pinned Locked Moved Unsolved General and Desktop
deletelaterqwidget
6 Posts 3 Posters 2.7k 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.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by
    #1

    Hey

    Loosing my marbles here... simple program:

    #include "QWidget";
    
    class A {
    public:
        virtual ~A() {
            std::cout << "~A\n";
        }
    };
    class aX {
    public:
        virtual ~aX() {
            std::cout << "~aX\n";
        }
    };
    
    class B : A {
    public:
        virtual ~B() override {
            std::cout << "~B\n";
        }
    };
    
    class C : public QWidget, public B, public aX {
    public:
        ~C() {
            B::~B();
            aX::~aX();
            std::cout << "~C\n";
        }
    };
    
    int main(int argc, char *argv[]) {
        qInstallMessageHandler(icDisplayDebugHandler);
        QApplication a(argc, argv);
        C *c = new C;
        c->deleteLater();
        QWidget w;
        w.show();
        return a.exec();
    

    The C never gets deleted, what do I do to delete it ?!
    I want to remove my subclassed widget from layout & delete it, but I just keep on hoging more and more memory :- (

    TIA

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What version of Qt ?
      On what OS ?
      How did you determine that it is not happening ?

      Working fine on macOS, with Qt 5.13
      I had to remove your custom message handling since you didn't provide it though.

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

      D 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        What version of Qt ?
        On what OS ?
        How did you determine that it is not happening ?

        Working fine on macOS, with Qt 5.13
        I had to remove your custom message handling since you didn't provide it though.

        D Offline
        D Offline
        Dariusz
        wrote on last edited by
        #3

        @SGaist Yeah seems to work... Whhhh frustrating !

        The message handling was skewing my test sigh.

        Ok so my error must be else where... if I want to remove widget & delete it... do I do this ?

        for (auto *widgetObj:mDisplayDataWidgetPtr) { // widgetObj = my base class that has a QWidget* as member.
            widgetObj->getWidgetPtr()->setParent(nullptr);
            mLay_content->removeWidget(widgetObj->getWidgetPtr());
            widgetObj->getWidgetPtr()->deleteLater();
            //delete widgetObj;
        }
        qDeleteAll(mDisplayDataWidgetPtr);
        mDisplayDataWidgetPtr.clear();
        

        Would this function call deleteLater properly on that widget I just removed and it would remove all widgets from its layout/child layouts?

        I'm struggling to clean up my complex gui : -(

        TIA

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Why isn't that member widget deletion handled by the destructor of the class you store in mDisplayDataWidgetPtr ?

          If I may, the mDisplayDataWidgetPtr name doesn't look like a list/vector/whatever which your code suggests it is. It's pretty confusing when reading your code. You might want to change it for something meaningful, it will help a lot for long term maintenance.

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

          D 1 Reply Last reply
          1
          • SGaistS SGaist

            Why isn't that member widget deletion handled by the destructor of the class you store in mDisplayDataWidgetPtr ?

            If I may, the mDisplayDataWidgetPtr name doesn't look like a list/vector/whatever which your code suggests it is. It's pretty confusing when reading your code. You might want to change it for something meaningful, it will help a lot for long term maintenance.

            D Offline
            D Offline
            Dariusz
            wrote on last edited by
            #5

            @SGaist Yup I agree should have called it better...

            QVector<dataWidget*> mDisplayDataWidgetPtr;
                class datWidget{
                    QWidget *mWidget;
                public:
                    datWidget();
                    ~datWidget()
                    QWidget*getWidgetPtr(){return mWidget;}
                }
            
            

            The question I have is...
            when I do:

            widgetObj->getWidgetPtr()->setParent(nullptr);
                mLay_content->removeWidget(widgetObj->getWidgetPtr());
                widgetObj->getWidgetPtr()->deleteLater();
            

            will that delete mWidget from the class above, making it a invalid pointer/which is fine as then I just do delete mDisplayDataWidgetPtr[x]; or better just do qDeleteAll(mDisplayDataWidgetPtr);

            Or do I need to delete it manually in dataWidget class?

            I'm getting random errors when I try to delete items manually... I think they get delete call before/after qt starts deleting them and I end up with crash...

            TIA

            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You should really read the chapter about the parent/child relationship here: https://doc.qt.io/qt-5/qobject.html#details

              ".... The parent takes ownership of the object; i.e., it will automatically delete its children in its destructor"

              So if they are all children of your mWidget then they will get deleted when mWidget is destroyed.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1

              • Login

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