Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

DeleteLater never deletes...



  • 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


  • Lifetime Qt Champion

    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.



  • @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


  • Lifetime Qt Champion

    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.



  • @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


  • Qt Champions 2019

    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.


Log in to reply